home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / tcl / tcl67.lha / tcl6.7 / doc / Tcl.n < prev    next >
Text File  |  1993-01-31  |  117KB  |  2,833 lines

  1. '\"
  2. '\" Copyright 1989-1992 Regents of the University of California
  3. '\" Permission to use, copy, modify, and distribute this
  4. '\" documentation for any purpose and without fee is hereby
  5. '\" granted, provided that this notice appears in all copies.
  6. '\" The University of California makes no representations about
  7. '\" the suitability of this material for any purpose.  It is
  8. '\" provided "as is" without express or implied warranty.
  9. '\" 
  10. '\" $Header: /user6/ouster/tcl/man/RCS/Tcl.n,v 1.105 93/01/31 15:35:41 ouster Exp $ SPRITE (Berkeley)
  11. '
  12. .so man.macros
  13. .de UL
  14. \\$1\l'|0\(ul'\\$2
  15. ..
  16. .HS Tcl tcl
  17. .BS
  18. .SH NAME
  19. Tcl \- overview of tool command language facilities
  20. .BE
  21.  
  22. .SH INTRODUCTION
  23. .PP
  24. Tcl stands for ``tool command language'' and is pronounced ``tickle.''
  25. It is actually two things:
  26. a language and a library.
  27. First, Tcl is a simple textual language,
  28. intended primarily for issuing commands to interactive programs such
  29. as text editors, debuggers, illustrators, and shells.  It has
  30. a simple syntax and is also programmable, so
  31. Tcl users can write command procedures to provide more powerful
  32. commands than those in the built-in set.
  33. .PP
  34. Second, Tcl is a library package that can be embedded in application
  35. programs.  The Tcl library consists of a parser for the Tcl
  36. language, routines to implement the Tcl built-in commands, and
  37. procedures that allow each application to extend Tcl with additional
  38. commands specific to that application.  The application program
  39. generates Tcl commands and passes them to the Tcl parser for
  40. execution.  Commands may be generated
  41. by reading characters from an input
  42. source, or by associating command strings with elements of the
  43. application's user interface, such as menu entries, buttons, or
  44. keystrokes.
  45. When the Tcl library receives commands it parses them
  46. into component fields and executes built-in commands directly.
  47. For commands implemented by the
  48. application, Tcl calls back to the application to execute the
  49. commands.  In many cases commands will invoke recursive invocations
  50. of the Tcl interpreter by passing in additional strings to execute
  51. (procedures, looping commands, and conditional commands all work
  52. in this way).
  53. .PP
  54. An application program gains three advantages by using Tcl for
  55. its command language.  First, Tcl provides a standard syntax:  once
  56. users know Tcl, they will be able to issue commands easily
  57. to any Tcl-based application.  Second, Tcl provides programmability.
  58. All a Tcl application needs to do is to implement a few
  59. application-specific low-level commands.  Tcl provides many utility
  60. commands plus a general programming interface for building up
  61. complex command procedures.  By using Tcl, applications need not
  62. re-implement these features.  Third, Tcl can be used as
  63. .VS
  64. a common language for communicating between applications.
  65. Inter-application communication is not built into the Tcl core
  66. described here, but various add-on libraries, such as the Tk toolkit,
  67. allow applications to issue commands to each other.
  68. This makes it possible for applications to work together in much
  69. more powerful ways than was previously possible.
  70. .VE
  71. .PP
  72. This manual page focuses primarily on the Tcl language.  It describes
  73. the language syntax and the built-in commands that will be available in
  74. any application based on Tcl.  The individual library
  75. procedures are described in more detail in separate manual pages, one
  76. per procedure.
  77.  
  78. .SH "INTERPRETERS"
  79. .PP
  80. The central data structure in Tcl is an interpreter (C type
  81. ``Tcl_Interp'').  An interpreter consists of a set of command
  82. bindings, a set of variable values, and a few other miscellaneous
  83. pieces of state.  Each Tcl command is interpreted in the context
  84. of a particular interpreter.
  85. Some Tcl-based applications will maintain
  86. multiple interpreters simultaneously, each associated with a
  87. different widget or portion of the application.
  88. Interpreters are relatively lightweight structures.  They can
  89. be created and deleted quickly, so application programmers should feel free to
  90. use multiple interpreters if that simplifies the application.
  91. Eventually Tcl will provide a mechanism for sending Tcl commands
  92. and results back and forth between interpreters, even if the
  93. interpreters are managed by different processes.
  94.  
  95. .SH "DATA TYPES"
  96. .PP
  97. Tcl supports only one type of data:  strings.  All commands,
  98. all arguments to commands, all command results, and all variable values
  99. are strings.
  100. Where commands require numeric arguments or return numeric results,
  101. the arguments and results are passed as strings.
  102. Many commands expect their string arguments to have certain formats,
  103. but this interpretation is
  104. up to the individual commands.  For example, arguments often contain
  105. Tcl command strings, which may get executed as part of the commands.
  106. The easiest way to understand the Tcl interpreter is to remember that
  107. everything is just an operation on a string.  In many cases Tcl constructs
  108. will look similar to more structured constructs from other languages.
  109. However, the Tcl constructs
  110. are not structured at all; they are just strings of characters, and this
  111. gives them a different behavior than the structures they may look like.
  112. .PP
  113. Although the exact interpretation of a Tcl string depends on who is
  114. doing the interpretation, there are three common forms that strings
  115. take:  commands, expressions, and lists.  The major sections below
  116. discuss these three forms in more detail.
  117.  
  118. .SH "BASIC COMMAND SYNTAX"
  119. .PP
  120. The Tcl language has syntactic similarities to both the Unix shells
  121. and Lisp.  However, the interpretation of commands is different
  122. in Tcl than in either of those other two systems.
  123. A Tcl command string consists of one or more commands separated
  124. by newline characters or semi-colons.
  125. Each command consists of a collection of fields separated by
  126. white space (spaces or tabs).
  127. The first field must be the name of a command, and the
  128. additional fields, if any, are arguments that will be passed to
  129. that command.  For example, the command
  130. .DS
  131. \fBset a 22\fR
  132. .DE
  133. has three fields:  the first, \fBset\fR, is the name of a Tcl command, and
  134. the last two, \fBa\fR and \fB22\fR, will be passed as arguments to
  135. the \fBset\fR command.  The command name may refer either to a built-in
  136. Tcl command, an application-specific command bound in with the library
  137. procedure \fBTcl_CreateCommand\fR, or a command procedure defined with the
  138. \fBproc\fR built-in command.
  139. Arguments are passed literally as
  140. text strings.  Individual commands may interpret those strings in any
  141. fashion they wish.  The \fBset\fR command, for example, will treat its
  142. first argument as the name of a variable and its second argument as a
  143. string value to assign to that variable.  For other commands arguments
  144. may be interpreted as integers, lists, file names, or Tcl commands.
  145. .PP
  146. .VS
  147. Command names should normally be typed completely (e.g. no abbreviations).
  148. However, if the Tcl interpreter cannot locate a command it invokes a
  149. special command named \fBunknown\fR which attempts to find or create
  150. the command.
  151. For example, at many sites \fBunknown\fR will search
  152. through library directories for the desired command and create it
  153. as a Tcl procedure if it is found.
  154. The \fBunknown\fR command often provides automatic completion of
  155. abbreviated commands, but usually only for commands that were typed
  156. interactively.
  157. It's probably a bad idea to use abbreviations in command scripts
  158. and other forms that will be re-used over time:  changes
  159. to the command set may cause abbreviations to become ambiguous,
  160. resulting in scripts that no longer work.
  161. .VE
  162.  
  163. .SH "COMMENTS"
  164. .PP
  165. If the first non-blank character in a command is \fB#\fR, then everything
  166. from the \fB#\fR up through the next newline character is treated as
  167. a comment and ignored.  When comments are embedded inside nested
  168. commands (e.g. fields enclosed in braces) they must have properly-matched
  169. braces (this is necessary because when Tcl parses the top-level command
  170. it doesn't yet know that the nested field will be used as a command so
  171. it cannot process the nested comment character as a comment).
  172.  
  173. .SH "GROUPING ARGUMENTS WITH DOUBLE-QUOTES"
  174. .PP
  175. Normally each argument field ends at the next white space, but
  176. double-quotes may be used to create arguments with embedded
  177. space.  If an argument
  178. field begins with a double-quote, then the argument isn't
  179. terminated by white space (including newlines) or a semi-colon
  180. (see below for information on semi-colons); instead it ends at the next
  181. double-quote character.  The double-quotes are not included
  182. in the resulting argument.  For example, the
  183. command
  184. .DS
  185. \fBset a "This is a single argument"\fR
  186. .DE
  187. will pass two arguments to \fBset\fR:  \fBa\fR and
  188. \fBThis is a single argument\fR.  Within double-quotes, command
  189. substitutions, variable substitutions, and backslash substitutions
  190. still occur, as described below.  If the first character of a
  191. command field is not a quote, then quotes receive no special
  192. interpretation in the parsing of that field.
  193.  
  194. .SH "GROUPING ARGUMENTS WITH BRACES"
  195. .PP
  196. Curly braces may also be used for grouping arguments.  They are
  197. similar to quotes except for two differences.  First, they nest;
  198. this makes them easier to use for complicated arguments like nested Tcl
  199. command strings.  Second, the substitutions described below for
  200. commands, variables, and backslashes do \fInot\fR occur in arguments
  201. enclosed in braces, so braces can be used to prevent substitutions
  202. where they are undesirable.
  203. If an argument field
  204. begins with a left brace, then the argument ends at the matching
  205. right brace.  Tcl will strip off the outermost layer of braces
  206. and pass the information between the braces to the command without
  207. any further modification.  For example, in the command
  208. .DS
  209. \fBset a {xyz a {b c d}}\fR
  210. .DE
  211. the \fBset\fR command will receive two arguments: \fBa\fR
  212. and \fBxyz a {b c d}\fR.
  213. .PP
  214. When braces or quotes are in effect, the matching brace
  215. or quote need not be on
  216. the same line as the starting quote or brace; in this case
  217. the newline will be
  218. included in the argument field along with any other characters up to the
  219. matching brace or quote.  For example, the \fBeval\fR command
  220. takes one
  221. argument, which is a command string; \fBeval\fR invokes the Tcl
  222. interpreter to execute the command string.  The command
  223. .DS
  224. \fBeval {
  225.     set a 22
  226.     set b 33
  227. }\fR
  228. .DE
  229. will assign the value \fB22\fR to \fBa\fR and \fB33\fR to \fBb\fR.
  230. .PP
  231. If the first character of a command field is not a left
  232. brace, then neither left nor right
  233. braces in the field will be treated specially (except as part of
  234. variable substitution; see below).
  235.  
  236. .SH "COMMAND SUBSTITUTION WITH BRACKETS"
  237. .PP
  238. If an open bracket occurs in a field of a command, then
  239. command substitution occurs (except for fields enclosed in
  240. braces).  All of the text up to the matching
  241. close bracket is treated as a Tcl command and executed immediately.
  242. Then the result of that command is substituted for the bracketed
  243. text.  For example, consider the command
  244. .DS
  245. \fBset a [set b]\fR
  246. .DE
  247. When the \fBset\fR command has only a single argument, it is the
  248. name of a variable and \fBset\fR returns the contents of that
  249. variable.  In this case, if variable \fBb\fR has the value \fBfoo\fR,
  250. then the command above is equivalent to the command
  251. .DS
  252. \fBset a foo\fR
  253. .DE
  254. Brackets can be used in more complex ways.  For example, if the
  255. variable \fBb\fR has the value \fBfoo\fR and the variable \fBc\fR
  256. has the value \fBgorp\fR, then the command
  257. .DS
  258. \fBset a xyz[set b].[set c]\fR
  259. .DE
  260. is equivalent to the command
  261. .DS
  262. \fBset a xyzfoo.gorp\fR
  263. .DE
  264. .VS
  265. A bracketed command may contain multiple commands separated by
  266. newlines or semi-colons in the usual fashion.
  267. In this case the value of the last command is used for substitution.
  268. For example, the command
  269. .DS
  270. \fBset a x[set b 22
  271. expr $b+2]x\fR
  272. .DE
  273. is equivalent to the command
  274. .DS
  275. \fBset a x24x\fR
  276. .DE
  277. .VE
  278. If a field is enclosed in braces then the brackets and the characters
  279. between them are not interpreted specially; they are passed through
  280. to the argument verbatim.
  281.  
  282. .SH "VARIABLE SUBSTITUTION WITH $"
  283. .PP
  284. The dollar sign (\fB$\fR) may be used as a special shorthand form
  285. for substituting variable values.
  286. If \fB$\fR appears in an argument that isn't enclosed in braces
  287. then variable substitution will occur.  The characters after
  288. the \fB$\fR, up to the first character that isn't a number, letter, or
  289. underscore, are taken as a variable name and the string value of that
  290. variable is substituted for the name.
  291. .VS
  292. For example, if variable \fBfoo\fR
  293. has the value \fBtest\fR, then the command
  294. .DS C
  295. \fBset a $foo.c\fR
  296. .DE
  297. is equivalent to the command
  298. .DS C
  299. \fBset a test.c\fR
  300. .DE
  301. .PP
  302. There are two special forms for variable substitution.
  303. If the next character after the name of the variable is an
  304. open parenthesis, then the variable is assumed to be an array
  305. name, and all of the characters between the open parenthesis
  306. and the next close parenthesis are taken as an index into the array.
  307. Command substitutions and variable substitutions are
  308. performed on the information between the parentheses before it is
  309. used as an index.
  310. For example, if the variable \fBx\fR is an array with one element
  311. named \fBfirst\fR and value \fB87\fR and another element named
  312. \fB14\fR and value \fBmore\fR, then the command
  313. .DS C
  314. \fBset a xyz$x(first)zyx
  315. .DE
  316. is equivalent to the command
  317. .DS C
  318. \fBset a xyz87zyx\fR
  319. .DE
  320. If the variable \fBindex\fR has the value \fB14\fR, then the command
  321. .DS C
  322. \fBset a xyz$x($index)zyx
  323. .DE
  324. is equivalent to the command
  325. .DS C
  326. \fBset a xyzmorezyx
  327. .DE
  328. For more information on arrays, see VARIABLES AND ARRAYS below.
  329. .PP
  330. The second special form for variables occurs when
  331. the dollar sign is followed by an open curly brace.
  332. In this case the variable name consists of all the characters
  333. up to the next curly brace.
  334. Array references are not possible in this form:  the name
  335. between braces is assumed to refer to a scalar variable.
  336. For example, if variable \fBfoo\fR has the value \fBtest\fR,
  337. then the command
  338. .DS C
  339. \fBset a abc${foo}bar\fR
  340. .DE
  341. is equivalent to the command
  342. .DS C
  343. \fBset a abctestbar\fR
  344. .DE
  345. .VE
  346. Variable substitution does not occur in arguments that are enclosed
  347. in braces:  the
  348. dollar sign and variable name are passed through to the argument verbatim.
  349. .PP
  350. The dollar sign abbreviation is simply a shorthand form.  \fB$a\fR is
  351. completely equivalent to \fB[set a]\fR; it is provided as a convenience
  352. to reduce typing.
  353.  
  354. .SH "SEPARATING COMMANDS WITH SEMI-COLONS"
  355. .PP
  356. Normally, each command occupies one line (the command is terminated by
  357. a newline character).  However, semi-colon (``;'') is treated
  358. as a command separator character; multiple commands may be placed
  359. on one line by separating them with a semi-colon.  Semi-colons are
  360. not treated as command separators if they appear within curly braces
  361. or double-quotes.
  362.  
  363. .SH "BACKSLASH SUBSTITUTION"
  364. .PP
  365. Backslashes may be used to insert non-printing characters into
  366. command fields and also to insert special characters like
  367. braces and brackets into fields
  368. without them being interpreted specially as described above.
  369. The backslash sequences understood by the Tcl interpreter are
  370. listed below.  In each case, the backslash
  371. sequence is replaced by the given character:
  372. .TP 20
  373. \fB\eb\fR
  374. Backspace (0x8).
  375. .TP 20
  376. \fB\ef\fR
  377. Form feed (0xc).
  378. .TP 20
  379. \fB\en\fR
  380. Newline (0xa).
  381. .TP 20
  382. \fB\er\fR
  383. Carriage-return (0xd).
  384. .TP 20
  385. \fB\et\fR
  386. Tab (0x9).
  387. .TP 20
  388. \fB\ev\fR
  389. Vertical tab (0xb).
  390. .TP 20
  391. \fB\e{\fR
  392. Left brace (``{'').
  393. .TP 20
  394. \fB\e}\fR
  395. Right brace (``}'').
  396. .TP 20
  397. \fB\e[\fR
  398. Open bracket (``['').
  399. .TP 20
  400. \fB\e]\fR
  401. Close bracket (``]'').
  402. .TP 20
  403. \fB\e$\fR
  404. Dollar sign (``$'').
  405. .TP 20
  406. \fB\e<space>\fR
  407. Space (`` ''): doesn't terminate argument.
  408. .br
  409. .TP 20
  410. \fB\e;\fR
  411. Semi-colon: doesn't terminate command.
  412. .TP 20
  413. \fB\e"\fR
  414. Double-quote.
  415. .TP 20
  416. \fB\e<newline>\fR
  417. Nothing:  this joins two lines together
  418. into a single line.  This backslash feature is unique in that
  419. it will be applied even when the sequence occurs within braces.
  420. .TP 20
  421. \fB\e\e\fR
  422. Backslash (``\e'').
  423. .TP 20
  424. \fB\e\fIddd\fR
  425. The digits \fIddd\fR (one, two, or three of them) give the octal value of
  426. the character.  Null characters may not be embedded in command fields;
  427. if \fIddd\fR is zero then the backslash sequence is ignored (i.e. it
  428. maps to an empty string).
  429. .PP
  430. For example, in the command
  431. .DS
  432. \fBset a \e{x\e[\e\0yz\e141\fR
  433. .DE
  434. the second argument to \fBset\fR will be ``\fB{x[\0yza\fR''.
  435. .PP
  436. If a backslash is followed by something other than one of the options
  437. described above, then the backslash is transmitted to the argument
  438. field without any special processing, and the Tcl scanner continues
  439. normal processing with the next character.  For example, in the
  440. command
  441. .DS
  442. \fBset \e*a \e\e\e{foo\fR
  443. .DE
  444. The first argument to \fBset\fR will be \fB\e*a\fR and the second
  445. argument will be \fB\e{foo\fR.
  446. .PP
  447. If an argument is enclosed in braces, then backslash sequences inside
  448. the argument are parsed but no substitution occurs (except for
  449. backslash-newline):  the backslash
  450. sequence is passed through to the argument as is, without making
  451. any special interpretation of the characters in the backslash sequence.
  452. In particular, backslashed braces are not counted in locating the
  453. matching right brace that terminates the argument.
  454. For example, in the
  455. command
  456. .DS
  457. \fBset a {\e{abc}\fR
  458. .DE
  459. the second argument to \fBset\fR will be \fB\e{abc\fR.
  460. .PP
  461. This backslash mechanism is not sufficient to generate absolutely
  462. any argument structure; it only covers the
  463. most common cases.  To produce particularly complicated arguments
  464. it is probably easiest to use the \fBformat\fR command along with
  465. command substitution.
  466.  
  467. .SH "COMMAND SUMMARY"
  468. .IP [1]
  469. A command is just a string.
  470. .IP [2]
  471. Within a string commands are separated by newlines or semi-colons
  472. (unless the newline or semi-colon is within braces or brackets
  473. or is backslashed).
  474. .IP [3]
  475. A command consists of fields.  The first field is the name of the command.
  476. The other fields are strings that are passed to that command as arguments.
  477. .IP [4]
  478. Fields are normally separated by white space.
  479. .IP [5]
  480. Double-quotes allow white space and semi-colons to appear within
  481. a single argument.
  482. Command substitution, variable substitution, and backslash substitution
  483. still occur inside quotes.
  484. .IP [6]
  485. Braces defer interpretation of special characters.
  486. If a field begins with a left brace, then it consists of everything
  487. between the left brace and the matching right brace. The
  488. braces themselves are not included in the argument.
  489. No further processing is done on the information between the braces
  490. except that backslash-newline sequences are eliminated.
  491. .IP [7]
  492. If a field doesn't begin with a brace then backslash,
  493. variable, and command substitution are done on the field.  Only a
  494. single level of processing is done:  the results of one substitution
  495. are not scanned again for further substitutions or any other
  496. special treatment.  Substitution can
  497. occur on any field of a command, including the command name
  498. as well as the arguments.
  499. .IP [8]
  500. If the first non-blank character of a command is a \fB#\fR, everything
  501. from the \fB#\fR up through the next newline is treated as a comment
  502. and ignored.
  503.  
  504. .SH "EXPRESSIONS"
  505. .VS
  506. .PP
  507. The second major interpretation applied to strings in Tcl is
  508. as expressions.  Several commands, such as \fBexpr\fR, \fBfor\fR,
  509. and \fBif\fR, treat one or more of their arguments as expressions
  510. and call the Tcl expression processors (\fBTcl_ExprLong\fR,
  511. \fBTcl_ExprBoolean\fR, etc.) to evaluate them.
  512. The operators permitted in Tcl expressions are a subset of
  513. the operators permitted in C expressions, and they have the
  514. same meaning and precedence as the corresponding C operators.
  515. Expressions almost always yield numeric results
  516. (integer or floating-point values).
  517. For example, the expression
  518. .DS
  519. \fB8.2 + 6\fR
  520. .DE
  521. evaluates to 14.2.
  522. Tcl expressions differ from C expressions in the way that
  523. operands are specified, and in that Tcl expressions support
  524. non-numeric operands and string comparisons.
  525. .PP
  526. A Tcl expression consists of a combination of operands, operators,
  527. and parentheses.
  528. White space may be used between the operands and operators and
  529. parentheses; it is ignored by the expression processor.
  530. Where possible, operands are interpreted as integer values.
  531. Integer values may be specified in decimal (the normal case), in octal (if the
  532. first character of the operand is \fB0\fR), or in hexadecimal (if the first
  533. two characters of the operand are \fB0x\fR).
  534. If an operand does not have one of the integer formats given
  535. above, then it is treated as a floating-point number if that is
  536. possible.  Floating-point numbers may be specified in any of the
  537. ways accepted by an ANSI-compliant C compiler (except that the
  538. ``f'', ``F'', ``l'', and ``L'' suffixes will not be permitted in
  539. most installations).  For example, all of the
  540. following are valid floating-point numbers:  2.1, 3., 6e4, 7.91e+16.
  541. If no numeric interpretation is possible, then an operand is left
  542. as a string (and only a limited set of operators may be applied to
  543. it).
  544. .PP
  545. Operands may be specified in any of the following ways:
  546. .IP [1]
  547. As an numeric value, either integer or floating-point.
  548. .IP [2]
  549. As a Tcl variable, using standard \fB$\fR notation.
  550. The variable's value will be used as the operand.
  551. .IP [3]
  552. As a string enclosed in double-quotes.
  553. The expression parser will perform backslash, variable, and
  554. command substitutions on the information between the quotes,
  555. and use the resulting value as the operand
  556. .IP [4]
  557. As a string enclosed in braces.
  558. The characters between the open brace and matching close brace
  559. will be used as the operand without any substitutions.
  560. .IP [5]
  561. As a Tcl command enclosed in brackets.
  562. The command will be executed and its result will be used as
  563. the operand.
  564. .LP
  565. Where substitutions occur above (e.g. inside quoted strings), they
  566. are performed by the expression processor.
  567. However, an additional layer of substitution may already have
  568. been performed by the command parser before the expression
  569. processor was called.
  570. As discussed below, it is usually best to enclose expressions
  571. in braces to prevent the command parser from performing substitutions
  572. on the contents.
  573. .PP
  574. For some examples of simple expressions, suppose the variable
  575. \fBa\fR has the value 3 and
  576. the variable \fBb\fR has the value 6.
  577. Then the expression on the left side of each of the lines below
  578. will evaluate to the value on the right side of the line:
  579. .DS
  580. .ta 6c
  581. \fB3.1 + $a    6.1
  582. 2 + "$a.$b"    5.6
  583. 4*[llength "6 2"]    8
  584. {word one} < "word $a"    0\fR
  585. .DE
  586. .PP
  587. The valid operators are listed below, grouped in decreasing order
  588. of precedence:
  589. .TP 20
  590. \fB\-\0\0~\0\0!\fR
  591. Unary minus, bit-wise NOT, logical NOT.  None of these operands
  592. may be applied to string operands, and bit-wise NOT may be
  593. applied only to integers.
  594. .TP 20
  595. \fB*\0\0/\0\0%\fR
  596. Multiply, divide, remainder.  None of these operands may be
  597. applied to string operands, and remainder may be applied only
  598. to integers.
  599. .TP 20
  600. \fB+\0\0\-\fR
  601. Add and subtract.  Valid for any numeric operands.
  602. .TP 20
  603. \fB<<\0\0>>\fR
  604. Left and right shift.  Valid for integer operands only.
  605. .TP 20
  606. \fB<\0\0>\0\0<=\0\0>=\fR
  607. Boolean less, greater, less than or equal, and greater than or equal.
  608. Each operator produces 1 if the condition is true, 0 otherwise.
  609. These operators may be applied to strings as well as numeric operands,
  610. in which case string comparison is used.
  611. .TP 20
  612. \fB==\0\0!=\fR
  613. Boolean equal and not equal.  Each operator produces a zero/one result.
  614. Valid for all operand types.
  615. .TP 20
  616. \fB&\fR
  617. Bit-wise AND.  Valid for integer operands only.
  618. .TP 20
  619. \fB^\fR
  620. Bit-wise exclusive OR.  Valid for integer operands only.
  621. .TP 20
  622. \fB|\fR
  623. Bit-wise OR.  Valid for integer operands only.
  624. .TP 20
  625. \fB&&\fR
  626. Logical AND.  Produces a 1 result if both operands are non-zero, 0 otherwise.
  627. Valid for numeric operands only (integers or floating-point).
  628. .TP 20
  629. \fB||\fR
  630. Logical OR.  Produces a 0 result if both operands are zero, 1 otherwise.
  631. Valid for numeric operands only (integers or floating-point).
  632. .TP 20
  633. \fIx\fB?\fIy\fB:\fIz\fR
  634. If-then-else, as in C.  If \fIx\fR
  635. evaluates to non-zero, then the result is the value of \fIy\fR.
  636. Otherwise the result is the value of \fIz\fR.
  637. The \fIx\fR operand must have a numeric value.
  638. .LP
  639. See the C manual for more details on the results
  640. produced by each operator.
  641. All of the binary operators group left-to-right within the same
  642. precedence level.  For example, the expression
  643. .DS
  644. \fB4*2 < 7\fR
  645. .DE
  646. evaluates to 0.
  647. .PP
  648. The \fB&&\fP, \fB||\fP, and \fB?:\fP operators have ``lazy
  649. evaluation'', just as in C, 
  650. which means that operands are not evaluated if they are
  651. not needed to determine the outcome.  For example, in
  652. .DS
  653. \fB$v ? [a] : [b]\fR
  654. .DE
  655. only one of \fB[a]\fR or \fB[b]\fR will actually be evaluated,
  656. depending on the value of \fB$v\fP.
  657. .PP
  658. All internal computations involving integers are done with the C type
  659. \fIlong\fP, and all internal computations involving floating-point are
  660. done with the C type \fIdouble\fP.
  661. When converting a string to floating-point, exponent overflow is
  662. detected and results in a Tcl error.
  663. For conversion to integer from string, detection of overflow depends
  664. on the behavior of some routines in the local C library, so it should
  665. be regarded as unreliable.
  666. In any case, overflow and underflow are generally not detected
  667. reliably for intermediate results.
  668. .PP
  669. Conversion among internal representations for integer, floating-point,
  670. and string operands is done automatically as needed.
  671. For arithmetic computations, integers are used until some
  672. floating-point number is introduced, after which floating-point is used.
  673. For example,
  674. .DS
  675. \fB5 / 4\fR
  676. .DE
  677. yields the result 1, while
  678. .DS
  679. \fB5 / 4.0\fR
  680. \fB5 / ( [string length "abcd"] + 0.0 )
  681. .DE
  682. both yield the result 1.25.
  683. .PP
  684. String values may be used as operands of the comparison operators,
  685. although the expression evaluator tries to do comparisons as integer
  686. or floating-point when it can.
  687. If one of the operands of a comparison is a string and the other
  688. has a numeric value, the numeric operand is converted back to
  689. a string using the C \fIsprintf\fP format specifier
  690. \fB%d\fR for integers and \fB%g\fR for floating-point values.
  691. For example, the expressions
  692. .DS
  693. \fB"0x03" > "2"\fR
  694. \fB"0y" < "0x12"\fR
  695. .DE
  696. both evaluate to 1.  The first comparison is done using integer
  697. comparison, and the second is done using string comparison after
  698. the second operand is converted to the string ``18''.
  699. .VE
  700. .PP
  701. In general it is safest to enclose an expression in braces when
  702. entering it in a command:  otherwise, if the expression contains
  703. any white space then the Tcl interpreter will split it
  704. among several arguments.  For example, the command
  705. .DS C
  706. \fBexpr $a + $b\fR
  707. .DE
  708. results in three arguments being passed to \fBexpr\fR:  \fB$a\fR,
  709. \fB+\fR, and \fB$b\fR.  In addition, if the expression isn't in braces
  710. then the Tcl interpreter will perform variable and command substitution
  711. immediately (it will happen in the command parser rather than in
  712. the expression parser).  In many cases the expression is being
  713. passed to a command that will evaluate the expression later (or
  714. even many times if, for example, the expression is to be used to
  715. decide when to exit a loop).  Usually the desired goal is to re-do
  716. the variable or command substitutions each time the expression is
  717. evaluated, rather than once and for all at the beginning.  For example,
  718. the command
  719. .DS C
  720. .ta 7c
  721. \fBfor {set i 1} $i<=10 {incr i} {...}\fR    *** WRONG ***
  722. .DE
  723. is probably intended to iterate over all values of \fBi\fR from 1 to 10.
  724. After each iteration of the body of the loop, \fBfor\fR will pass
  725. its second argument to the expression evaluator to see whether or not
  726. to continue processing.  Unfortunately, in this case the value of \fBi\fR
  727. in the second argument will be substituted once and for all when the
  728. \fBfor\fR command is parsed.  If \fBi\fR was 0 before the \fBfor\fR
  729. command was invoked then \fBfor\fR's second argument will be \fB0<=10\fR
  730. which will always evaluate to 1, even though \fBi\fR's value eventually
  731. becomes greater than 10.  In the above case the loop will never
  732. terminate.  Instead, the expression should be placed in braces:
  733. .DS C
  734. .ta 7c
  735. \fBfor {set i 1} {$i<=10} {incr i} {...}\fR    *** RIGHT ***
  736. .DE
  737. This causes the substitution of \fBi\fR's
  738. value to be delayed; it will be re-done each time the expression is
  739. evaluated, which is the desired result.
  740.  
  741. .SH LISTS
  742. .PP
  743. The third major way that strings are interpreted in Tcl is as lists.
  744. A list is just a string with a list-like structure
  745. consisting of fields separated by white space.  For example, the
  746. string
  747. .DS
  748. \fBAl Sue Anne John\fR
  749. .DE
  750. is a list with four elements or fields.
  751. Lists have the same basic structure as command strings, except
  752. that a newline character in a list is treated as a field separator
  753. just like space or tab.  Conventions for braces and quotes
  754. and backslashes are the same for lists as for commands.  For example,
  755. the string
  756. .DS
  757. \fBa b\e c {d e {f g h}}\fR
  758. .DE
  759. is a list with three elements:  \fBa\fR, \fBb c\fR, and \fBd e {f g h}\fR.
  760. Whenever an element
  761. is extracted from a list, the same rules about braces and quotes and
  762. backslashes are applied as for commands.  Thus in the example above
  763. when the third element is extracted from the list, the result is
  764. .DS
  765. \fBd e {f g h}\fR
  766. .DE
  767. (when the field was extracted, all that happened was to strip off
  768. the outermost layer of braces).  Command substitution and
  769. variable substitution are never
  770. made on a list (at least, not by the list-processing commands; the
  771. list can always be passed to the Tcl interpreter for evaluation).
  772. .PP
  773. The Tcl commands \fBconcat\fR, \fBforeach\fR, 
  774. .VS
  775. \fBlappend\fR, \fBlindex\fR, \fBlinsert\fR, \fBlist\fR, \fBllength\fR,
  776. \fBlrange\fR, \fBlreplace\fR, \fBlsearch\fR, and \fBlsort\fR allow
  777. you to build lists,
  778. .VE
  779. extract elements from them, search them, and perform other list-related
  780. functions.
  781.  
  782. .SH "REGULAR EXPRESSIONS"
  783. .VS
  784. .PP
  785. Tcl provides two commands that support string matching using
  786. \fBegrep\fR-style regular expressions: \fBregexp\fR and \fBregsub\fR.
  787. Regular expressions are implemented using Henry Spencer's package,
  788. and the description of regular expressions below is copied verbatim
  789. from his manual entry.
  790. .PP
  791. A regular expression is zero or more \fIbranches\fR, separated by ``|''.
  792. It matches anything that matches one of the branches.
  793. .PP
  794. A branch is zero or more \fIpieces\fR, concatenated.
  795. It matches a match for the first, followed by a match for the second, etc.
  796. .PP
  797. A piece is an \fIatom\fR possibly followed by ``*'', ``+'', or ``?''.
  798. An atom followed by ``*'' matches a sequence of 0 or more matches of the atom.
  799. An atom followed by ``+'' matches a sequence of 1 or more matches of the atom.
  800. An atom followed by ``?'' matches a match of the atom, or the null string.
  801. .PP
  802. An atom is a regular expression in parentheses (matching a match for the
  803. regular expression), a \fIrange\fR (see below), ``.''
  804. (matching any single character), ``^'' (matching the null string at the
  805. beginning of the input string), ``$'' (matching the null string at the
  806. end of the input string), a ``\e'' followed by a single character (matching
  807. that character), or a single character with no other significance
  808. (matching that character).
  809. .PP
  810. A \fIrange\fR is a sequence of characters enclosed in ``[]''.
  811. It normally matches any single character from the sequence.
  812. If the sequence begins with ``^'',
  813. it matches any single character \fInot\fR from the rest of the sequence.
  814. If two characters in the sequence are separated by ``\-'', this is shorthand
  815. for the full list of ASCII characters between them
  816. (e.g. ``[0-9]'' matches any decimal digit).
  817. To include a literal ``]'' in the sequence, make it the first character
  818. (following a possible ``^'').
  819. To include a literal ``\-'', make it the first or last character.
  820. .PP
  821. If a regular expression could match two different parts of a string,
  822. it will match the one which begins earliest.
  823. If both begin in the same place but match different lengths, or match
  824. the same length in different ways, life gets messier, as follows.
  825. .PP
  826. In general, the possibilities in a list of branches are considered in
  827. left-to-right order, the possibilities for ``*'', ``+'', and ``?'' are
  828. considered longest-first, nested constructs are considered from the
  829. outermost in, and concatenated constructs are considered leftmost-first.
  830. The match that will be chosen is the one that uses the earliest
  831. possibility in the first choice that has to be made.
  832. If there is more than one choice, the next will be made in the same manner
  833. (earliest possibility) subject to the decision on the first choice.
  834. And so forth.
  835. .PP
  836. For example, ``(ab|a)b*c'' could match ``abc'' in one of two ways.
  837. The first choice is between ``ab'' and ``a''; since ``ab'' is earlier, and does
  838. lead to a successful overall match, it is chosen.
  839. Since the ``b'' is already spoken for,
  840. the ``b*'' must match its last possibility\(emthe empty string\(emsince
  841. it must respect the earlier choice.
  842. .PP
  843. In the particular case where no ``|''s are present and there is only one
  844. ``*'', ``+'', or ``?'', the net effect is that the longest possible
  845. match will be chosen.
  846. So ``ab*'', presented with ``xabbbby'', will match ``abbbb''.
  847. Note that if ``ab*'' is tried against ``xabyabbbz'', it
  848. will match ``ab'' just after ``x'', due to the begins-earliest rule.
  849. (In effect, the decision on where to start the match is the first choice
  850. to be made, hence subsequent choices must respect it even if this leads them
  851. to less-preferred alternatives.)
  852. .VE
  853.  
  854. .SH "COMMAND RESULTS"
  855. .PP
  856. Each command produces two results:  a code and a string.  The
  857. code indicates whether the command completed successfully or not,
  858. and the string gives additional information.  The valid codes are
  859. defined in tcl.h, and are:
  860. .RS
  861. .TP 20
  862. \fBTCL_OK\fR
  863. This is the normal return code, and indicates that the command completed
  864. successfully.  The string gives the command's return value.
  865. .TP 20
  866. \fBTCL_ERROR\fR
  867. Indicates that an error occurred; the string gives a message describing
  868. the error.
  869. .VS
  870. In addition, the global variable \fBerrorInfo\fR will contain
  871. human-readable information
  872. describing which commands and procedures were being executed when the
  873. error occurred, and the global variable \fBerrorCode\fR will contain
  874. machine-readable details about the error, if they are available.
  875. See the section BUILT-IN VARIABLES below for more information.
  876. .VE
  877. .VE
  878. .TP 20
  879. \fBTCL_RETURN\fR
  880. Indicates that the \fBreturn\fR command has been invoked, and that the
  881. current procedure (or top-level command or \fBsource\fR command)
  882. should return immediately.  The
  883. string gives the return value for the procedure or command.
  884. .TP 20
  885. \fBTCL_BREAK\fR
  886. Indicates that the \fBbreak\fR command has been invoked, so the
  887. innermost loop should abort immediately.  The string should always
  888. be empty.
  889. .TP 20
  890. \fBTCL_CONTINUE\fR
  891. Indicates that the \fBcontinue\fR command has been invoked, so the
  892. innermost loop should go on to the next iteration.  The string
  893. should always be empty.
  894. .RE
  895. Tcl programmers do not normally need to think about return codes,
  896. since TCL_OK is almost always returned.  If anything else is returned
  897. by a command, then the Tcl interpreter immediately stops processing
  898. commands and returns to its caller.  If there are several nested
  899. invocations of the Tcl interpreter in progress, then each nested
  900. command will usually return the error to its caller, until eventually
  901. the error is reported to the top-level application code.  The
  902. application will then display the error message for the user.
  903. .PP
  904. In a few cases, some commands will handle certain ``error'' conditions
  905. themselves and not return them upwards.  For example, the \fBfor\fR
  906. command checks for the TCL_BREAK code; if it occurs, then \fBfor\fR
  907. stops executing the body of the loop and returns TCL_OK to its
  908. caller.  The \fBfor\fR command also handles TCL_CONTINUE codes and the
  909. procedure interpreter handles TCL_RETURN codes.  The \fBcatch\fR
  910. command allows Tcl programs to catch errors and handle them without
  911. aborting command interpretation any further.
  912.  
  913. .SH PROCEDURES
  914. .PP
  915. Tcl allows you to extend the command interface by defining
  916. procedures.  A Tcl procedure can be invoked just like any other Tcl
  917. command (it has a name and it receives one or more arguments).
  918. The only difference is that its body isn't a piece of C code linked
  919. into the program; it is a string containing one or more other
  920. Tcl commands.  See the \fBproc\fR command for information on
  921. how to define procedures and what happens when they are invoked.
  922.  
  923. .SH VARIABLES \- SCALARS AND ARRAYS
  924. .VS
  925. .PP
  926. Tcl allows the definition of variables and the use of their values
  927. either through \fB$\fR-style variable substitution, the \fBset\fR
  928. command, or a few other mechanisms.
  929. Variables need not be declared:  a new variable will automatically
  930. be created each time a new variable name is used.
  931. .PP
  932. Tcl supports two types of variables:  scalars and arrays.
  933. A scalar variable has a single value, whereas an array variable
  934. can have any number of elements, each with a name (called
  935. its ``index'') and a value.
  936. Array indexes may be arbitrary strings; they need not be numeric.
  937. Parentheses are used refer to array elements in Tcl commands.
  938. For example, the command
  939. .DS C
  940. \fBset x(first) 44\fR
  941. .DE
  942. will modify the element of \fBx\fR whose index is \fBfirst\fR
  943. so that its new value is \fB44\fR.
  944. Two-dimensional arrays can be simulated in Tcl by using indexes
  945. that contain multiple concatenated values.
  946. For example, the commands
  947. .DS C
  948. \fBset a(2,3) 1\fR
  949. \fBset a(3,6) 2\fR
  950. .DE
  951. set the elements of \fBa\fR whose indexes are \fB2,3\fR and \fB3,6\fR.
  952. .PP
  953. In general, array elements may be used anywhere in Tcl that scalar
  954. variables may be used.
  955. If an array is defined with a particular name, then there may
  956. not be a scalar variable with the same name.
  957. Similarly, if there is a scalar variable with a particular
  958. name then it is not possible to make array references to the
  959. variable.
  960. To convert a scalar variable to an array or vice versa, remove
  961. the existing variable with the \fBunset\fR command.
  962. .PP
  963. The \fBarray\fR command provides several features for dealing
  964. with arrays, such as querying the names of all the elements of
  965. the array and searching through the array one element at a time.
  966. .VE
  967. .PP
  968. Variables may be either global or local.  If a variable
  969. name is used when a procedure isn't being executed, then it
  970. automatically refers to a global variable.  Variable names used
  971. within a procedure normally refer to local variables associated with that
  972. invocation of the procedure.  Local variables are deleted whenever
  973. a procedure exits.  The \fBglobal\fR command may be used to request
  974. that a name refer to a global variable for the duration of the current
  975. procedure (this is somewhat analogous to \fBextern\fR in C).
  976.  
  977. .SH "BUILT-IN COMMANDS"
  978. .PP
  979. The Tcl library provides the following built-in commands, which will
  980. be available in any application using Tcl.  In addition to these
  981. built-in commands, there may be additional commands defined by each
  982. application, plus commands defined as Tcl procedures.
  983. In the command syntax descriptions below, words in boldface are
  984. literals that you type verbatim to Tcl.
  985. Words in italics are meta-symbols; they serve as names for any of
  986. a range of values that you can type.
  987. Optional arguments or groups of arguments are indicated by enclosing them
  988. in question-marks.
  989. Ellipses (``...'') indicate that any number of additional
  990. arguments or groups of arguments may appear, in the same format
  991. as the preceding argument(s).
  992. .TP
  993. \fBappend \fIvarName value \fR?\fIvalue value ...\fR?
  994. .VS
  995. Append all of the \fIvalue\fR arguments to the current value
  996. of variable \fIvarName\fR.  If \fIvarName\fR doesn't exist,
  997. it is given a value equal to the concatenation of all the
  998. \fIvalue\fR arguments.
  999. This command provides an efficient way to build up long
  1000. variables incrementally.
  1001. For example, ``\fBappend a $b\fR'' is much more efficient than
  1002. ``\fBset a $a$b\fR'' if \fB$a\fR is long.
  1003. .VE
  1004. .TP
  1005. \fBarray \fIoption arrayName\fR ?\fIarg arg ...\fR?
  1006. .VS
  1007. This command performs one of several operations on the
  1008. variable given by \fIarrayName\fR.
  1009. \fIArrayName\fR must be the name of an existing array variable.
  1010. The \fIoption\fR argument determines what action is carried
  1011. out by the command.
  1012. The legal \fIoptions\fR (which may be abbreviated) are:
  1013. .RS
  1014. .TP
  1015. \fBarray anymore \fIarrayName searchId\fR
  1016. Returns 1 if there are any more elements left to be processed
  1017. in an array search, 0 if all elements have already been
  1018. returned.
  1019. \fISearchId\fR indicates which search on \fIarrayName\fR to
  1020. check, and must have been the return value from a previous
  1021. invocation of \fBarray startsearch\fR.
  1022. This option is particularly useful if an array has an element
  1023. with an empty name, since the return value from
  1024. \fBarray nextelement\fR won't indicate whether the search
  1025. has been completed.
  1026. .TP
  1027. \fBarray donesearch \fIarrayName searchId\fR
  1028. This command terminates an array search and destroys all the
  1029. state associated with that search.  \fISearchId\fR indicates
  1030. which search on \fIarrayName\fR to destroy, and must have
  1031. been the return value from a previous invocation of
  1032. \fBarray startsearch\fR.  Returns an empty string.
  1033. .TP
  1034. \fBarray names \fIarrayName\fR
  1035. Returns a list containing the names of all of the elements in
  1036. the array.
  1037. If there are no elements in the array then an empty string is
  1038. returned.
  1039. .TP
  1040. \fBarray nextelement \fIarrayName searchId\fR
  1041. Returns the name of the next element in \fIarrayName\fR, or
  1042. an empty string if all elements of \fIarrayName\fR have
  1043. already been returned in this search.  The \fIsearchId\fR
  1044. argument identifies the search, and must have
  1045. been the return value of an \fBarray startsearch\fR command.
  1046. Warning:  if elements are added to or deleted from the array,
  1047. then all searches are automatically terminated just as if
  1048. \fBarray donesearch\fR had been invoked; this will cause
  1049. \fBarray nextelement\fR operations to fail for those searches.
  1050. .TP
  1051. \fBarray size \fIarrayName\fR
  1052. Returns a decimal string giving the number of elements in the
  1053. array.
  1054. .TP
  1055. \fBarray startsearch \fIarrayName\fR
  1056. This command initializes an element-by-element search through the
  1057. array given by \fIarrayName\fR, such that invocations of the
  1058. \fBarray nextelement\fR command will return the names of the
  1059. individual elements in the array.
  1060. When the search has been completed, the \fBarray donesearch\fR
  1061. command should be invoked.
  1062. The return value is a
  1063. search identifier that must be used in \fBarray nextelement\fR
  1064. and \fBarray donesearch\fR commands; it allows multiple
  1065. searches to be underway simultaneously for the same array.
  1066. .VE
  1067. .RE
  1068. .TP
  1069. \fBbreak\fR
  1070. This command may be invoked only inside the body of a loop command
  1071. such as \fBfor\fR or \fBforeach\fR or \fBwhile\fR.  It returns a TCL_BREAK code
  1072. to signal the innermost containing loop command to return immediately.
  1073. .TP
  1074. \fBcase\fI string \fR?\fBin\fR? \fIpatList body \fR?\fIpatList body \fR...?
  1075. .TP
  1076. \fBcase\fI string \fR?\fBin\fR? {\fIpatList body \fR?\fIpatList body \fR...?}
  1077. Match \fIstring\fR against each of the \fIpatList\fR arguments
  1078. in order.  If one matches, then evaluate the following \fIbody\fR argument
  1079. by passing it recursively to the Tcl interpreter, and return the result
  1080. of that evaluation.  Each \fIpatList\fR argument consists of a single
  1081. pattern or list of patterns.  Each pattern may contain any of the wild-cards
  1082. described under \fBstring match\fR.  If a \fIpatList\fR
  1083. argument is \fBdefault\fR, the corresponding body will be evaluated
  1084. if no \fIpatList\fR matches \fIstring\fR.  If no \fIpatList\fR argument
  1085. matches \fIstring\fR and no default is given, then the \fBcase\fR
  1086. command returns an empty string.
  1087. .RS
  1088. .PP
  1089. Two syntaxes are provided.
  1090. The first uses a separate argument for each of the patterns and commands;
  1091. this form is convenient if substitutions are desired on some of the
  1092. patterns or commands.
  1093. .VS
  1094. The second form places all of the patterns and commands together into
  1095. a single argument; the argument must have proper list structure, with
  1096. the elements of the list being the patterns and commands.
  1097. The second form makes it easy to construct multi-line case commands,
  1098. since the braces around the whole list make it unnecessary to include a
  1099. backslash at the end of each line.
  1100. Since the \fIpatList\fR arguments are in braces in the second form,
  1101. no command or variable substitutions are performed on them;  this makes
  1102. the behavior of the second form different than the first form in some
  1103. cases.
  1104. .PP
  1105. Below are some examples of \fBcase\fR commands:
  1106. .DS
  1107. \fBcase abc in {a b} {format 1} default {format 2} a* {format 3}
  1108. .DE
  1109. will return \fB3\fR, 
  1110. .DS
  1111. .ta .5c 1c
  1112. \fBcase a in {
  1113.     {a b} {format 1}
  1114.     default {format 2}
  1115.     a* {format 3}
  1116. }
  1117. .DE
  1118. will return \fB1\fR, and
  1119. .DS
  1120. .ta .5c 1c
  1121. \fBcase xyz {
  1122.     {a b}
  1123.         {format 1}
  1124.     default
  1125.         {format 2}
  1126.     a*
  1127.         {format 3}
  1128. }
  1129. .DE
  1130. will return \fB2\fR.
  1131. .VE
  1132. .RE
  1133. .TP
  1134. \fBcatch\fI command \fR?\fIvarName\fR?
  1135. The \fBcatch\fR command may be used to prevent errors from aborting
  1136. command interpretation.  \fBCatch\fR calls the Tcl interpreter recursively
  1137. to execute \fIcommand\fR, and always returns a TCL_OK code, regardless of
  1138. any errors that might occur while executing \fIcommand\fR.  The return
  1139. value from \fBcatch\fR is a decimal string giving the
  1140. code returned by the Tcl interpreter after executing \fIcommand\fR.
  1141. This will be \fB0\fR (TCL_OK) if there were no errors in \fIcommand\fR; otherwise
  1142. it will have a non-zero value corresponding to one of the exceptional
  1143. return codes (see tcl.h for the definitions of code values).  If the
  1144. \fIvarName\fR argument is given, then it gives the name of a variable;
  1145. \fBcatch\fR will set the value of the variable to the string returned
  1146. from \fIcommand\fR (either a result or an error message).
  1147. .TP
  1148. \fBcd \fR?\fIdirName\fR?
  1149. .VS
  1150. Change the current working directory to \fIdirName\fR, or to the
  1151. home directory (as specified in the HOME environment variable) if
  1152. \fIdirName\fR is not given.
  1153. If \fIdirName\fR starts with a tilde, then tilde-expansion is
  1154. done as described for \fBTcl_TildeSubst\fR.
  1155. Returns an empty string.
  1156. This command can potentially be disruptive to an application,
  1157. so it may be removed in some applications.
  1158. .TP
  1159. \fBclose \fIfileId\fR
  1160. Closes the file given by \fIfileId\fR.
  1161. \fIFileId\fR must be the return value from a previous invocation
  1162. of the \fBopen\fR command; after this command, it should not be
  1163. used anymore.
  1164. If \fIfileId\fR refers to a command pipeline instead of a file,
  1165. then \fBclose\fR waits for the children to complete.
  1166. The normal result of this command is an empty string, but errors
  1167. are returned if there are problems in closing the file or waiting
  1168. for children to complete.
  1169. .VE
  1170. .TP
  1171. \fBconcat\fI arg \fR?\fIarg ...\fR?
  1172. This command treats each argument as a list and concatenates them
  1173. into a single list.  It permits any number of arguments.  For example,
  1174. the command
  1175. .RS
  1176. .DS
  1177. \fBconcat a b {c d e} {f {g h}}\fR
  1178. .DE
  1179. will return
  1180. .DS
  1181. \fBa b c d e f {g h}\fR
  1182. .DE
  1183. as its result.
  1184. .RE
  1185. .TP
  1186. \fBcontinue\fR
  1187. This command may be invoked only inside the body of a loop command
  1188. such as \fBfor\fR or \fBforeach\fR or \fBwhile\fR.  It
  1189. returns a  TCL_CONTINUE code
  1190. to signal the innermost containing loop command to skip the
  1191. remainder of the loop's body
  1192. but continue with the next iteration of the loop.
  1193. .TP
  1194. \fBeof \fIfileId\fR
  1195. .VS
  1196. Returns 1 if an end-of-file condition has occurred on \fIfileId\fR,
  1197. 0 otherwise.
  1198. \fIFileId\fR must have been the return
  1199. value from a previous call to \fBopen\fR, or it may be \fBstdin\fR,
  1200. \fBstdout\fR, or \fBstderr\fR to refer to one of the standard I/O
  1201. channels.
  1202. .VE
  1203. .TP
  1204. \fBerror \fImessage\fR ?\fIinfo\fR? ?\fIcode\fR?
  1205. Returns a TCL_ERROR code, which causes command interpretation to be
  1206. unwound.  \fIMessage\fR is a string that is returned to the application
  1207. to indicate what went wrong.
  1208. .RS
  1209. .PP
  1210. If the \fIinfo\fR argument is provided and is non-empty,
  1211. it is used to initialize the global variable \fBerrorInfo\fR.
  1212. \fBerrorInfo\fR is used to accumulate a stack trace of what
  1213. was in progress when an error occurred; as nested commands unwind,
  1214. the Tcl interpreter adds information to \fBerrorInfo\fR.  If the
  1215. \fIinfo\fR argument is present, it is used to initialize
  1216. \fBerrorInfo\fR and the first increment of unwind information
  1217. will not be added by the Tcl interpreter.  In other
  1218. words, the command containing the \fBerror\fR command will not appear
  1219. in \fBerrorInfo\fR; in its place will be \fIinfo\fR.
  1220. This feature is most useful in conjunction with the \fBcatch\fR command:
  1221. if a caught error cannot be handled successfully, \fIinfo\fR can be used
  1222. to return a stack trace reflecting the original point of occurrence
  1223. of the error:
  1224. .DS
  1225. \fBcatch {...} errMsg
  1226. set savedInfo $errorInfo
  1227. \&...
  1228. error $errMsg $savedInfo\fR
  1229. .DE
  1230. .PP
  1231. .VS
  1232. If the \fIcode\fR argument is present, then its value is stored
  1233. in the \fBerrorCode\fR global variable.  This variable is intended
  1234. to hold a machine-readable description of the error in cases where
  1235. such information is available; see the section BUILT-IN VARIABLES
  1236. below for information on the proper format for the variable.
  1237. If the \fIcode\fR argument is not
  1238. present, then \fBerrorCode\fR is automatically reset to
  1239. ``NONE'' by the Tcl interpreter as part of processing the
  1240. error generated by the command.
  1241. .VE
  1242. .RE
  1243. .TP
  1244. \fBeval \fIarg \fR?\fIarg ...\fR?
  1245. \fBEval\fR takes one or more arguments, which together comprise a Tcl
  1246. command (or collection of Tcl commands separated by newlines in the
  1247. usual way).  \fBEval\fR concatenates all its arguments in the same
  1248. fashion as the \fBconcat\fR command, passes the concatenated string to the
  1249. Tcl interpreter recursively, and returns the result of that
  1250. evaluation (or any error generated by it).
  1251. .TP
  1252. \fBexec \fIarg \fR?\fIarg ...\fR?
  1253. .VS
  1254. This command treats its arguments as the specification
  1255. of one or more UNIX commands to execute as subprocesses.
  1256. The commands take the form of a standard shell pipeline;
  1257. ``|'' arguments separate commands in the
  1258. pipeline and cause standard output of the preceding command
  1259. to be piped into standard input of the next command.
  1260. .RS
  1261. .PP
  1262. Under normal conditions the result of the \fBexec\fR command
  1263. consists of the standard output produced by the last command
  1264. in the pipeline.
  1265. If any of the commands in the pipeline exit abnormally or
  1266. are killed or suspended, then \fBexec\fR will return an error
  1267. and the error message will include the pipeline's output followed by
  1268. error messages describing the abnormal terminations; the
  1269. \fBerrorCode\fR variable will contain additional information
  1270. about the last abnormal termination encountered.
  1271. If any of the commands writes to its standard error file,
  1272. then \fBexec\fR will return an error, and the error message
  1273. will include the pipeline's output, followed by messages
  1274. about abnormal terminations (if any), followed by the standard error
  1275. output.
  1276. .PP
  1277. If the last character of the result or error message
  1278. is a newline then that character is deleted from the result
  1279. or error message for consistency with normal
  1280. Tcl return values.
  1281. .PP
  1282. If an \fIarg\fR has the value ``>'' then the
  1283. following argument is taken as the name of a file and
  1284. the standard output of the last command in the pipeline
  1285. is redirected to the file.  In this situation \fBexec\fR
  1286. will normally return an empty string.
  1287. .PP
  1288. If an \fIarg\fR has the value ``<'' then the following
  1289. argument is taken as the name of a file to use
  1290. for standard input to the first command in the
  1291. pipeline.
  1292. If an argument has the value ``<<'' then the following
  1293. argument is taken as an immediate value to be passed to
  1294. the first command as standard input.
  1295. If there is no ``<'' or ``<<'' argument then the standard
  1296. input for the first command in the pipeline is taken from
  1297. the application's current standard input.
  1298. .PP
  1299. If the last \fIarg\fR is ``&'' then the command will be
  1300. executed in background.
  1301. In this case the standard output from the last command
  1302. in the pipeline will
  1303. go to the application's standard output unless
  1304. redirected in the command, and error output from all
  1305. the commands in the pipeline will go to the application's
  1306. standard error file.
  1307. .PP
  1308. Each \fIarg\fR becomes one word for a command, except for
  1309. ``|'', ``<'', ``<<'', ``>'', and ``&'' arguments, and the
  1310. arguments that follow ``<'', ``<<'', and ``>''.
  1311. The first word in each command is taken as the command name;
  1312. tilde-substitution is performed on it, and the directories
  1313. in the PATH environment variable are searched for
  1314. an executable by the given name.
  1315. No ``glob'' expansion or other shell-like substitutions
  1316. are performed on the arguments to commands.
  1317. .RE
  1318. .TP
  1319. \fBexit \fR?returnCode\fR?
  1320. Terminate the process, returning \fIreturnCode\fR to the
  1321. parent as the exit status.
  1322. If \fIreturnCode\fR isn't specified then it defaults
  1323. to 0.
  1324. .VE
  1325. .TP
  1326. \fBexpr \fIarg\fR
  1327. Calls the expression processor to evaluate \fIarg\fR, and returns
  1328. the result as a string.  See the section EXPRESSIONS above.
  1329. .TP
  1330. \fBfile \fIoption\fR \fIname\fR ?\fIarg arg ...\fR?
  1331. .VS
  1332. Operate on a file or a file name.  \fIName\fR is the name of a file;
  1333. if it starts with a tilde, then tilde substitution is done before
  1334. executing the command (see the manual entry for \fBTcl_TildeSubst\fR
  1335. for details).
  1336. \fIOption\fR indicates what to do with the file name.  Any unique
  1337. abbreviation for \fIoption\fR is acceptable.  The valid options are:
  1338. .RS
  1339. .TP
  1340. \fBfile \fBatime \fIname\fR
  1341. Return a decimal string giving the time at which file \fIname\fR
  1342. was last accessed.  The time is measured in the standard UNIX
  1343. fashion as seconds from a fixed starting time (often January 1, 1970).
  1344. If the file doesn't exist or its access time cannot be queried then an
  1345. error is generated.
  1346. .TP
  1347. \fBfile \fBdirname \fIname\fR
  1348. Return all of the characters in \fIname\fR up to but not including
  1349. the last slash character.  If there are no slashes in \fIname\fR
  1350. then return ``.''.  If the last slash in \fIname\fR is its first
  1351. character, then return ``/''.
  1352. .TP
  1353. \fBfile \fBexecutable \fIname\fR
  1354. Return \fB1\fR if file \fIname\fR is executable by
  1355. the current user, \fB0\fR otherwise.
  1356. .TP
  1357. \fBfile \fBexists \fIname\fR
  1358. Return \fB1\fR if file \fIname\fR exists and the current user has
  1359. search privileges for the directories leading to it, \fB0\fR otherwise.
  1360. .TP
  1361. \fBfile \fBextension \fIname\fR
  1362. Return all of the characters in \fIname\fR after and including the
  1363. last dot in \fIname\fR.  If there is no dot in \fIname\fR then return
  1364. the empty string.
  1365. .TP
  1366. \fBfile \fBisdirectory \fIname\fR
  1367. Return \fB1\fR if file \fIname\fR is a directory,
  1368. \fB0\fR otherwise.
  1369. .TP
  1370. \fBfile \fBisfile \fIname\fR
  1371. Return \fB1\fR if file \fIname\fR is a regular file,
  1372. \fB0\fR otherwise.
  1373. .TP
  1374. \fBfile lstat \fIname varName\fR
  1375. Same as \fBstat\fR option (see below) except uses the \fIlstat\fR
  1376. kernel call instead of \fIstat\fR.  This means that if \fIname\fR
  1377. refers to a symbolic link the information returned in \fIvarName\fR
  1378. is for the link rather than the file it refers to.  On systems that
  1379. don't support symbolic links this option behaves exactly the same
  1380. as the \fBstat\fR option.
  1381. .TP
  1382. \fBfile \fBmtime \fIname\fR
  1383. Return a decimal string giving the time at which file \fIname\fR
  1384. was last modified.  The time is measured in the standard UNIX
  1385. fashion as seconds from a fixed starting time (often January 1, 1970).
  1386. If the file doesn't exist or its modified time cannot be queried then an
  1387. error is generated.
  1388. .TP
  1389. \fBfile \fBowned \fIname\fR
  1390. Return \fB1\fR if file \fIname\fR is owned by the current user,
  1391. \fB0\fR otherwise.
  1392. .TP
  1393. \fBfile \fBreadable \fIname\fR
  1394. Return \fB1\fR if file \fIname\fR is readable by
  1395. the current user, \fB0\fR otherwise.
  1396. .TP
  1397. \fBfile readlink \fIname\fR
  1398. Returns the value of the symbolic link given by \fIname\fR (i.e. the
  1399. name of the file it points to).  If
  1400. \fIname\fR isn't a symbolic link or its value cannot be read, then
  1401. an error is returned.  On systems that don't support symbolic links
  1402. this option is undefined.
  1403. .TP
  1404. \fBfile \fBrootname \fIname\fR
  1405. Return all of the characters in \fIname\fR up to but not including
  1406. the last ``.'' character in the name.  If \fIname\fR doesn't contain
  1407. a dot, then return \fIname\fR.
  1408. .TP
  1409. \fBfile \fBsize \fIname\fR
  1410. Return a decimal string giving the size of file \fIname\fR in bytes.
  1411. If the file doesn't exist or its size cannot be queried then an
  1412. error is generated.
  1413. .TP
  1414. \fBfile \fBstat  \fIname varName\fR
  1415. Invoke the \fBstat\fR kernel call on \fIname\fR, and use the
  1416. variable given by \fIvarName\fR to hold information returned from
  1417. the kernel call.
  1418. \fIVarName\fR is treated as an array variable,
  1419. and the following elements of that variable are set: \fBatime\fR,
  1420. \fBctime\fR, \fBdev\fR, \fBgid\fR, \fBino\fR, \fBmode\fR, \fBmtime\fR,
  1421. \fBnlink\fR, \fBsize\fR, \fBtype\fR, \fBuid\fR.
  1422. Each element except \fBtype\fR is a decimal string with the value of
  1423. the corresponding field from the \fBstat\fR return structure; see the
  1424. manual entry for \fBstat\fR for details on the meanings of the values.
  1425. The \fBtype\fR element gives the type of the file in the same form
  1426. returned by the command \fBfile type\fR.
  1427. This command returns an empty string.
  1428. .TP
  1429. \fBfile \fBtail \fIname\fR
  1430. Return all of the characters in \fIname\fR after the last slash.
  1431. If \fIname\fR contains no slashes then return \fIname\fR.
  1432. .TP
  1433. \fBfile \fBtype \fIname\fR
  1434. Returns a string giving the type of file \fIname\fR, which will be
  1435. one of \fBfile\fR, \fBdirectory\fR, \fBcharacterSpecial\fR,
  1436. \fBblockSpecial\fR, \fBfifo\fR, \fBlink\fR, or \fBsocket\fR.
  1437. .TP
  1438. \fBfile \fBwritable \fIname\fR
  1439. Return \fB1\fR if file \fIname\fR is writable by
  1440. the current user, \fB0\fR otherwise.
  1441. .RE
  1442. .IP
  1443. The \fBfile\fR commands that return 0/1 results are often used in
  1444. conditional or looping commands, for example:
  1445. .RS
  1446. .DS
  1447. \fBif {![file exists foo]} then {error {bad file name}} else {...}\fR
  1448. .DE
  1449. .VE
  1450. .RE
  1451. .TP
  1452. \fBflush \fIfileId\fR
  1453. .VS
  1454. Flushes any output that has been buffered for \fIfileId\fR.
  1455. \fIFileId\fR must have been the return
  1456. value from a previous call to \fBopen\fR, or it may be
  1457. \fBstdout\fR or \fBstderr\fR to access one of the standard I/O streams;
  1458. it must refer to a file that was opened for writing.
  1459. This command returns an empty string.
  1460. .VE
  1461. .TP
  1462. \fBfor \fIstart test next body\fR
  1463. \fBFor\fR is a looping command, similar in structure to the C
  1464. \fBfor\fR statement.  The \fIstart\fR, \fInext\fR, and
  1465. \fIbody\fR arguments must be Tcl command strings, and \fItest\fR
  1466. is an expression string.
  1467. The \fBfor\fR command first invokes the Tcl interpreter to
  1468. execute \fIstart\fR.  Then it repeatedly evaluates \fItest\fR as
  1469. an expression; if the result is non-zero it invokes the Tcl
  1470. interpreter on \fIbody\fR, then invokes the Tcl interpreter on \fInext\fR,
  1471. then repeats the loop.  The command terminates when \fItest\fR evaluates
  1472. to 0.  If a \fBcontinue\fR command is invoked within \fIbody\fR then
  1473. any remaining commands in the current execution of \fIbody\fR are skipped;
  1474. processing continues by invoking the Tcl interpreter on \fInext\fR, then
  1475. evaluating \fItest\fR, and so on.  If a \fBbreak\fR command is invoked
  1476. within \fIbody\fR
  1477. or \fInext\fR,
  1478. then the \fBfor\fR command will
  1479. return immediately.
  1480. The operation of \fBbreak\fR and \fBcontinue\fR are similar to the
  1481. corresponding statements in C.
  1482. \fBFor\fR returns an empty string.
  1483. .TP
  1484. \fBforeach \fIvarname list body\fR
  1485. In this command, \fIvarname\fR is the name of a variable, \fIlist\fR
  1486. is a list of values to assign to \fIvarname\fR, and \fIbody\fR is a
  1487. collection of Tcl commands.  For each field in \fIlist\fR (in order
  1488. from left to right), \fBforeach\fR assigns the contents of the
  1489. field to \fIvarname\fR (as if the \fBlindex\fR command had been used
  1490. to extract the field), then calls the Tcl interpreter to execute
  1491. \fIbody\fR.  The \fBbreak\fR and \fBcontinue\fR statements may be
  1492. invoked inside \fIbody\fR, with the same effect as in the \fBfor\fR
  1493. command.  \fBForeach\fR returns an empty string.
  1494. .TP
  1495. \fBformat \fIformatString \fR?\fIarg arg ...\fR?
  1496. This command generates a formatted string in the same way as the
  1497. C \fBsprintf\fR procedure (it uses \fBsprintf\fR in its
  1498. implementation).  \fIFormatString\fR indicates how to format
  1499. the result, using \fB%\fR fields as in \fBsprintf\fR, and the additional
  1500. arguments, if any, provide values to be substituted into the result.
  1501. All of the \fBsprintf\fR options are valid; see the \fBsprintf\fR
  1502. man page for details.  Each \fIarg\fR must match the expected type
  1503. from the \fB%\fR field in \fIformatString\fR; the \fBformat\fR command
  1504. converts each argument to the correct type (floating, integer, etc.)
  1505. before passing it to \fBsprintf\fR for formatting.
  1506. The only unusual conversion is for \fB%c\fR; in this case the argument
  1507. must be a decimal string, which will then be converted to the corresponding
  1508. ASCII character value.
  1509. \fBFormat\fR does backslash substitution on its \fIformatString\fR
  1510. argument, so backslash sequences in \fIformatString\fR will be handled
  1511. correctly even if the argument is in braces.
  1512. The return value from \fBformat\fR
  1513. is the formatted string.
  1514. .TP
  1515. \fBgets \fIfileId\fR ?\fIvarName\fR?
  1516. .VS
  1517. Reads the next line from the file given by \fIfileId\fR and discards
  1518. the terminating newline character.
  1519. If \fIvarName\fR is specified, then the line is placed in the variable
  1520. by that name and the return value is a count of the number of characters
  1521. read (not including the newline).
  1522. If the end of the file is reached before reading
  1523. any characters then \-1 is returned and \fIvarName\fR is set to an
  1524. empty string.
  1525. If \fIvarName\fR is not specified then the return value will be
  1526. the line (minus the newline character) or an empty string if
  1527. the end of the file is reached before reading any characters.
  1528. An empty string will also be returned if a line contains no characters
  1529. except the newline, so \fBeof\fR may have to be used to determine
  1530. what really happened.
  1531. If the last character in the file is not a newline character, then
  1532. \fBgets\fR behaves as if there were an additional newline character
  1533. at the end of the file.
  1534. \fIFileId\fR must be \fBstdin\fR or the return value from a previous
  1535. call to \fBopen\fR; it must refer to a file that was opened
  1536. for reading.
  1537. .VE
  1538. .TP
  1539. \fBglob \fR?\fB\-nocomplain\fR? \fIfilename\fR ?\fIfilename ...\fR?
  1540. This command performs filename globbing, using csh rules.  The returned
  1541. value from \fBglob\fR is the list of expanded filenames.
  1542. .VS
  1543. If \fB\-nocomplain\fR is specified as the first argument then an empty
  1544. list may be returned;  otherwise an error is returned if the expanded
  1545. list is empty.  The \fB\-nocomplain\fR argument must be provided
  1546. exactly: an abbreviation will not be accepted.
  1547. .VE
  1548. .TP
  1549. \fBglobal \fIvarname \fR?\fIvarname ...\fR?
  1550. This command is ignored unless a Tcl procedure is being interpreted.
  1551. If so, then it declares the given \fIvarname\fR's to be global variables
  1552. rather than local ones.  For the duration of the current procedure
  1553. (and only while executing in the current procedure), any reference to
  1554. any of the \fIvarname\fRs will be bound to a global variable instead
  1555. of a local one.
  1556. .TP
  1557. \fBhistory \fR?\fIoption\fR? ?\fIarg arg ...\fR?
  1558. Note:  this command may not be available in all Tcl-based applications.
  1559. Typically, only those that receive command input in a typescript
  1560. form will support history.
  1561. The \fBhistory\fR command performs one of several operations related to
  1562. recently-executed commands recorded in a history list.  Each of
  1563. these recorded commands is referred to as an ``event''.  When
  1564. specifying an event to the \fBhistory\fR command, the following
  1565. forms may be used:
  1566. .RS
  1567. .IP [1]
  1568. A number:  if positive, it refers to the event with
  1569. that number (all events are numbered starting at 1).  If the number
  1570. is negative, it selects an event relative to the current event
  1571. (\fB\-1\fR refers to the previous event, \fB\-2\fR to the one before that, and
  1572. so on).
  1573. .IP [2]
  1574. A string:  selects the most recent event that matches the string.
  1575. An event is considered to match the string either if the string is
  1576. the same as the first characters of the event, or if the string
  1577. matches the event in the sense of the \fBstring match\fR command.
  1578. .LP
  1579. The \fBhistory\fR command can take any of the following forms:
  1580. .TP
  1581. \fBhistory\fR
  1582. Same
  1583. .VS
  1584. as \fBhistory info\fR, described below.
  1585. .VE
  1586. .TP
  1587. \fBhistory add\fI command \fR?\fBexec\fR?
  1588. Add the \fIcommand\fR argument to the history list as a new event.  If
  1589. \fBexec\fR is specified (or abbreviated) then the command is also
  1590. executed and its result is returned.  If \fBexec\fR isn't specified
  1591. then an empty string is returned as result.
  1592. .TP
  1593. \fBhistory change\fI newValue\fR ?\fIevent\fR?
  1594. Replace the value recorded for an event with \fInewValue\fR.  \fIEvent\fR
  1595. specifies the event to replace, and
  1596. defaults to the \fIcurrent\fR event (not event \fB\-1\fR).  This command
  1597. is intended for use in commands that implement new forms of history
  1598. substitution and wish to replace the current event (which invokes the
  1599. substitution) with the command created through substitution.  The return
  1600. value is an empty string.
  1601. .TP
  1602. \fBhistory event\fR ?\fIevent\fR?
  1603. Returns the value of the event given by \fIevent\fR.  \fIEvent\fR
  1604. defaults to \fB\-1\fR.  This command causes history revision to occur:
  1605. see below for details.
  1606. .TP
  1607. \fBhistory info \fR?\fIcount\fR?
  1608. Returns a formatted string (intended for humans to read) giving
  1609. the event number and contents for each of the events in the history
  1610. list except the current event.  If \fIcount\fR is specified
  1611. then only the most recent \fIcount\fR events are returned.
  1612. .TP
  1613. \fBhistory keep \fIcount\fR
  1614. This command may be used to change the size of the history list to
  1615. \fIcount\fR events.  Initially, 20 events are retained in the history
  1616. list.  This command returns an empty string.
  1617. .TP
  1618. \fBhistory nextid\fR
  1619. Returns the number of the next event to be recorded
  1620. in the history list.  It is useful for things like printing the
  1621. event number in command-line prompts.
  1622. .TP
  1623. \fBhistory redo \fR?\fIevent\fR?
  1624. Re-execute the command indicated by \fIevent\fR and return its result.
  1625. \fIEvent\fR defaults to \fB\-1\fR.  This command results in history
  1626. revision:  see below for details.
  1627. .TP
  1628. \fBhistory substitute \fIold new \fR?\fIevent\fR?
  1629. Retrieve the command given by \fIevent\fR
  1630. (\fB\-1\fR by default), replace any occurrences of \fIold\fR by
  1631. \fInew\fR in the command (only simple character equality is supported;
  1632. no wild cards), execute the resulting command, and return the result
  1633. of that execution.  This command results in history
  1634. revision:  see below for details.
  1635. .TP
  1636. \fBhistory words \fIselector\fR ?\fIevent\fR?
  1637. Retrieve from the command given by \fIevent\fR (\fB\-1\fR by default)
  1638. the words given by \fIselector\fR, and return those words in a string
  1639. separated by spaces.  The \fBselector\fR argument has three forms.
  1640. If it is a single number then it selects the word given by that
  1641. number (\fB0\fR for the command name, \fB1\fR for its first argument,
  1642. and so on).  If it consists of two numbers separated by a dash,
  1643. then it selects all the arguments between those two.  Otherwise
  1644. \fBselector\fR is treated as a pattern; all words matching that
  1645. pattern (in the sense of \fBstring match\fR) are returned.  In
  1646. the numeric forms \fB$\fR may be used
  1647. to select the last word of a command.
  1648. For example, suppose the most recent command in the history list is
  1649. .RS
  1650. .DS
  1651. \fBformat  {%s is %d years old} Alice [expr $ageInMonths/12]\fR
  1652. .DE
  1653. Below are some history commands and the results they would produce:
  1654. .DS
  1655. .ta 4c
  1656. .fi
  1657. .UL Command "    "
  1658. .UL Result
  1659. .nf
  1660.  
  1661. \fBhistory words $    [expr $ageInMonths/12]\fR
  1662. \fBhistory words 1-2    {%s is %d years  old} Alice\fR
  1663. \fBhistory words *a*o*    {%s is %d years old} [expr $ageInMonths/12]\fR
  1664. .DE
  1665. \fBHistory words\fR results in history revision:  see below for details.
  1666. .RE
  1667. The history options \fBevent\fR, \fBredo\fR, \fBsubstitute\fR,
  1668. and \fBwords\fR result in ``history revision''.
  1669. When one of these options is invoked then the current event
  1670. is modified to eliminate the history command and replace it with
  1671. the result of the history command.
  1672. For example, suppose that the most recent command in the history
  1673. list is
  1674. .DS
  1675. \fBset a [expr $b+2]\fR
  1676. .DE
  1677. and suppose that the next command invoked is one of the ones on
  1678. the left side of the table below.  The command actually recorded in
  1679. the history event will be the corresponding one on the right side
  1680. of the table.
  1681. .ne 1.5c
  1682. .DS
  1683. .ta 4c
  1684. .fi
  1685. .UL "Command Typed" "    "
  1686. .UL "Command Recorded"
  1687. .nf
  1688.  
  1689. \fBhistory redo    set a [expr $b+2]\fR
  1690. \fBhistory s a b    set b [expr $b+2]\fR
  1691. \fBset c [history w 2]    set c [expr $b+2]\fR
  1692. .DE
  1693. .VS
  1694. History revision is needed because event specifiers like \fB\-1\fR
  1695. are only valid at a particular time:  once more events have been
  1696. added to the history list a different event specifier would be
  1697. needed.
  1698. History revision occurs even when \fBhistory\fR is invoked
  1699. indirectly from the current event (e.g. a user types a command
  1700. that invokes a Tcl procedure that invokes \fBhistory\fR):  the
  1701. top-level command whose execution eventually resulted in a
  1702. \fBhistory\fR command is replaced.
  1703. If you wish to invoke commands like \fBhistory words\fR without
  1704. history revision, you can use \fBhistory event\fR to save the
  1705. current history event and then use \fBhistory change\fR to
  1706. restore it later.
  1707. .VE
  1708. .RE
  1709. .TP
  1710. \fBif \fIexpr1 \fR?\fBthen\fR? \fIbody1 \fBelseif \fIexpr2 \fR?\fBthen\fR? \fIbody2\fR \fBelseif\fR ... \fR?\fBelse\fR? ?\fIbodyN\fR?
  1711. .VS
  1712. The \fIif\fR command evaluates \fIexpr1\fR as an expression (in the
  1713. same way that \fBexpr\fR evaluates its argument).  The value of the
  1714. expression must be numeric; if it
  1715. is non-zero then \fIbody1\fR is executed by passing it to the
  1716. Tcl interpreter.
  1717. Otherwise \fIexpr2\fR is evaluated as an expression and if it is non-zero
  1718. then \fBbody2\fR is executed, and so on.
  1719. If none of the expressions evaluates to non-zero then \fIbodyN\fR is
  1720. executed.
  1721. The \fBthen\fR and \fBelse\fR arguments are optional
  1722. ``noise words'' to make the command easier to read.
  1723. There may be any number of \fBelseif\fR clauses, including zero.
  1724. \fIBodyN\fR may also be omitted as long as \fBelse\fR is omitted too.
  1725. The return value from the command is the result of the body script
  1726. that was executed, or an empty string
  1727. if none of the expressions was non-zero and there was no \fIbodyN\fR.
  1728. .VE
  1729. .TP
  1730. \fBincr \fIvarName \fR?\fIincrement\fR?
  1731. .VS
  1732. Increment the value stored in the variable whose name is \fIvarName\fR.
  1733. The value of the variable must be integral.
  1734. If \fIincrement\fR is supplied then its value (which must be an
  1735. integer) is added to the value of variable \fIvarName\fR;  otherwise
  1736. 1 is added to \fIvarName\fR.
  1737. The new value is stored as a decimal string in variable \fIvarName\fR
  1738. and also returned as result.
  1739. .VE
  1740. .TP
  1741. \fBinfo \fIoption \fR?\fIarg arg ...\fR?
  1742. Provide information about various internals to the Tcl interpreter.
  1743. The legal \fIoption\fR's (which may be abbreviated) are:
  1744. .RS
  1745. .TP
  1746. \fBinfo args \fIprocname\fR
  1747. Returns a list containing the names of the arguments to procedure
  1748. \fIprocname\fR, in order.  \fIProcname\fR must be the name of a
  1749. Tcl command procedure.
  1750. .TP
  1751. \fBinfo body \fIprocname\fR
  1752. Returns the body of procedure \fIprocname\fR.  \fIProcname\fR must be
  1753. the name of a Tcl command procedure.
  1754. .TP
  1755. \fBinfo cmdcount\fR
  1756. Returns a count of the total number of commands that have been invoked
  1757. in this interpreter.
  1758. .TP
  1759. \fBinfo commands \fR?\fIpattern\fR?
  1760. If \fIpattern\fR isn't specified, returns a list of names of all the
  1761. Tcl commands, including both the built-in commands written in C and
  1762. the command procedures defined using the \fBproc\fR command.
  1763. If \fIpattern\fR is specified, only those names matching \fIpattern\fR
  1764. are returned.  Matching is determined using the same rules as for
  1765. \fBstring match\fR.
  1766. .TP
  1767. \fBinfo complete \fIcommand\fR
  1768. .VS
  1769. Returns 1 if \fIcommand\fR is a complete Tcl command in the sense of
  1770. having no unclosed quotes, braces, brackets or array element names,
  1771. If the command doesn't appear to be complete then 0 is returned.
  1772. This command is typically used in line-oriented input environments
  1773. to allow users to type in commands that span multiple lines;  if the
  1774. command isn't complete, the script can delay evaluating it until additional
  1775. lines have been typed to complete the command.
  1776. .VE
  1777. .TP
  1778. \fBinfo default \fIprocname arg varname\fR
  1779. \fIProcname\fR must be the name of a Tcl command procedure and \fIarg\fR
  1780. must be the name of an argument to that procedure.  If \fIarg\fR
  1781. doesn't have a default value then the command returns \fB0\fR.
  1782. Otherwise it returns \fB1\fR and places the default value of \fIarg\fR
  1783. into variable \fIvarname\fR.
  1784. .TP
  1785. \fBinfo exists \fIvarName\fR
  1786. Returns \fB1\fR if the variable named \fIvarName\fR exists in the
  1787. current context (either as a global or local variable), returns \fB0\fR
  1788. otherwise.
  1789. .TP
  1790. \fBinfo globals \fR?\fIpattern\fR?
  1791. If \fIpattern\fR isn't specified, returns a list of all the names
  1792. of currently-defined global variables.
  1793. If \fIpattern\fR is specified, only those names matching \fIpattern\fR
  1794. are returned.  Matching is determined using the same rules as for
  1795. \fBstring match\fR.
  1796. .TP
  1797. \fBinfo level\fR ?\fInumber\fR?
  1798. If \fInumber\fR is not specified, this command returns a number
  1799. giving the stack level of the invoking procedure, or 0 if the
  1800. command is invoked at top-level.  If \fInumber\fR is specified,
  1801. then the result is a list consisting of the name and arguments for the
  1802. procedure call at level \fInumber\fR on the stack.  If \fInumber\fR
  1803. is positive then it selects a particular stack level (1 refers
  1804. to the top-most active procedure, 2 to the procedure it called, and
  1805. so on); otherwise it gives a level relative to the current level
  1806. (0 refers to the current procedure, -1 to its caller, and so on).
  1807. See the \fBuplevel\fR command for more information on what stack
  1808. levels mean.
  1809. .TP
  1810. \fBinfo library\fR
  1811. .VS
  1812. Returns the name of the library directory in which standard Tcl
  1813. scripts are stored.
  1814. The default value for the library is compiled into Tcl, but it
  1815. .VS
  1816. may be overridden by setting the TCL_LIBRARY environment variable.
  1817. If there is no TCL_LIBRARY variable and no compiled-in value then
  1818. and error is generated.
  1819. .VE
  1820. See the \fBlibrary\fR manual entry for details of the facilities
  1821. provided by the Tcl script library.
  1822. Normally each application will have its own application-specific
  1823. script library in addition to the Tcl script library;  I suggest that
  1824. each application set a global variable with a name like
  1825. .VS
  1826. \fB$\fIapp\fB_library\fR (where \fIapp\fR is the application's name)
  1827. .VE
  1828. to hold the location of that application's library directory.
  1829. .VE
  1830. .TP
  1831. \fBinfo locals \fR?\fIpattern\fR?
  1832. If \fIpattern\fR isn't specified, returns a list of all the names
  1833. of currently-defined local variables, including arguments to the
  1834. current procedure, if any.
  1835. .VS
  1836. Variables defined with the \fBglobal\fR and \fBupvar\fR commands
  1837. will not be returned.
  1838. .VE
  1839. If \fIpattern\fR is specified, only those names matching \fIpattern\fR
  1840. are returned.  Matching is determined using the same rules as for
  1841. \fBstring match\fR.
  1842. .TP
  1843. \fBinfo procs \fR?\fIpattern\fR?
  1844. If \fIpattern\fR isn't specified, returns a list of all the
  1845. names of Tcl command procedures.
  1846. If \fIpattern\fR is specified, only those names matching \fIpattern\fR
  1847. are returned.  Matching is determined using the same rules as for
  1848. \fBstring match\fR.
  1849. .TP
  1850. \fBinfo script\fR
  1851. .VS
  1852. If a Tcl script file is currently being evaluated (i.e. there is a
  1853. call to \fBTcl_EvalFile\fR active or there is an active invocation
  1854. of the \fBsource\fR command), then this command returns the name
  1855. of the innermost file being processed.  Otherwise the command returns an
  1856. empty string.
  1857. .VE
  1858. .TP
  1859. \fBinfo tclversion\fR
  1860. Returns the version number for this version of Tcl in the form \fIx.y\fR,
  1861. where changes to \fIx\fR represent major changes with probable
  1862. incompatibilities and changes to \fIy\fR represent small enhancements and
  1863. bug fixes that retain backward compatibility.
  1864. .TP
  1865. \fBinfo vars\fR ?\fIpattern\fR?
  1866. If \fIpattern\fR isn't specified,
  1867. returns a list of all the names of currently-visible variables, including
  1868. both locals and currently-visible globals.
  1869. If \fIpattern\fR is specified, only those names matching \fIpattern\fR
  1870. are returned.  Matching is determined using the same rules as for
  1871. \fBstring match\fR.
  1872. .RE
  1873. .TP
  1874. \fBjoin \fIlist \fR?\fIjoinString\fR?
  1875. .VS
  1876. The \fIlist\fR argument must be a valid Tcl list.
  1877. This command returns the string
  1878. formed by joining all of the elements of \fIlist\fR together with
  1879. \fIjoinString\fR separating each adjacent pair of elements.
  1880. The \fIjoinString\fR argument defaults to a space character.
  1881. .VE
  1882. .TP
  1883. \fBlappend \fIvarName value \fR?\fIvalue value ...\fR?
  1884. .VS
  1885. Treat the variable given by \fIvarName\fR as a list and append
  1886. each of the \fIvalue\fR arguments to that list as a separate
  1887. element, with spaces between elements.
  1888. If \fIvarName\fR doesn't exist, it is created as a list with elements
  1889. given by the \fIvalue\fR arguments.
  1890. \fBLappend\fR is similar to \fBappend\fR except that the \fIvalue\fRs
  1891. are appended as list elements rather than raw text.
  1892. This command provides a relatively efficient way to build up
  1893. large lists.  For example, ``\fBlappend a $b\fR'' is much
  1894. more efficient than ``\fBset a [concat $a [list $b]]\fR'' when
  1895. \fB$a\fR is long.
  1896. .TP
  1897. \fBlindex \fIlist index\fR
  1898. Treats \fIlist\fR as a Tcl list and returns the \fIindex\fR'th element
  1899. from it (0 refers to the first element of the list).
  1900. In extracting the element, \fIlindex\fR observes the same rules
  1901. concerning braces and quotes and backslashes as the Tcl command
  1902. interpreter; however, variable
  1903. substitution and command substitution do not occur.
  1904. If \fIindex\fR is negative or greater than or equal to the number
  1905. of elements in \fIvalue\fR, then an empty
  1906. string is returned.
  1907. .TP
  1908. \fBlinsert \fIlist index element \fR?\fIelement element ...\fR?
  1909. This command produces a new list from \fIlist\fR by inserting all
  1910. of the \fIelement\fR arguments just before the \fIindex\fRth
  1911. element of \fIlist\fR.  Each \fIelement\fR argument will become
  1912. a separate element of the new list.  If \fIindex\fR is less than
  1913. or equal to zero, then the new elements are inserted at the
  1914. beginning of the list.  If \fIindex\fR is greater than or equal
  1915. to the number of elements in the list, then the new elements are
  1916. appended to the list.
  1917. .VE
  1918. .TP
  1919. \fBlist \fIarg \fR?\fIarg ...\fR?
  1920. This command returns a list comprised of all the \fIarg\fRs.  Braces
  1921. and backslashes get added as necessary, so that the \fBindex\fR command
  1922. may be used on the result to re-extract the original arguments, and also
  1923. so that \fBeval\fR may be used to execute the resulting list, with
  1924. \fIarg1\fR comprising the command's name and the other \fIarg\fRs comprising
  1925. its arguments.  \fBList\fR produces slightly different results than
  1926. \fBconcat\fR:  \fBconcat\fR removes one level of grouping before forming
  1927. the list, while \fBlist\fR works directly from the original arguments.
  1928. For example, the command
  1929. .RS
  1930. .DS
  1931. \fBlist a b {c d e} {f {g h}}
  1932. .DE
  1933. will return
  1934. .DS
  1935. \fBa b {c d e} {f {g h}}
  1936. .DE
  1937. while \fBconcat\fR with the same arguments will return
  1938. .DS
  1939. \fBa b c d e f {g h}\fR
  1940. .DE
  1941. .RE
  1942. .br
  1943. .VS
  1944. .TP
  1945. \fBllength \fIlist\fR
  1946. Treats \fIlist\fR as a list and returns a decimal string giving
  1947. the number of elements in it.
  1948. .TP
  1949. \fBlrange \fIlist first last
  1950. \fIList\fR must be a valid Tcl list.  This command will
  1951. return a new list consisting of elements
  1952. \fIfirst\fR through \fIlast\fR, inclusive.
  1953. \fILast\fR may be \fBend\fR (or any
  1954. abbreviation of it) to refer to the last element of the list.
  1955. If \fIfirst\fR is less than zero, it is treated as if it were zero.
  1956. If \fIlast\fR is greater than or equal to the number of elements
  1957. in the list, then it is treated as if it were \fBend\fR.
  1958. If \fIfirst\fR is greater than \fIlast\fR then an empty string
  1959. is returned.
  1960. Note: ``\fBlrange \fIlist first first\fR'' does not always produce the
  1961. same result as ``\fBlindex \fIlist first\fR'' (although it often does
  1962. for simple fields that aren't enclosed in braces); it does, however,
  1963. produce exactly the same results as ``\fBlist [lindex \fIlist first\fB]\fR''
  1964. .TP
  1965. \fBlreplace \fIlist first last \fR?\fIelement element ...\fR?
  1966. Returns a new list formed by replacing one or more elements of
  1967. \fIlist\fR with the \fIelement\fR arguments.
  1968. \fIFirst\fR gives the index in \fIlist\fR of the first element
  1969. to be replaced.
  1970. If \fIfirst\fR is less than zero then it refers to the first
  1971. element of \fIlist\fR;  the element indicated by \fIfirst\fR
  1972. must exist in the list.
  1973. \fILast\fR gives the index in \fIlist\fR of the last element
  1974. to be replaced;  it must be greater than or equal to \fIfirst\fR.
  1975. \fILast\fR may be \fBend\fR (or any abbreviation of it) to indicate
  1976. that all elements between \fIfirst\fR and the end of the list should
  1977. be replaced.
  1978. The \fIelement\fR arguments specify zero or more new arguments to
  1979. be added to the list in place of those that were deleted.
  1980. Each \fIelement\fR argument will become a separate element of
  1981. the list.
  1982. If no \fIelement\fR arguments are specified, then the elements
  1983. between \fIfirst\fR and \fIlast\fR are simply deleted.
  1984. .TP
  1985. \fBlsearch \fIlist pattern\fR
  1986. Search the elements of \fIlist\fR to see if one of them matches
  1987. \fIpattern\fR.
  1988. If so, the command returns the index of the first matching
  1989. element.
  1990. If not, the command returns \fB\-1\fR.
  1991. Pattern matching is done in the same way as for the \fBstring match\fR
  1992. command.
  1993. .TP
  1994. \fBlsort \fIlist\fR
  1995. Sort the elements of \fIlist\fR, returning a new list in sorted
  1996. order.
  1997. ASCII sorting is used, with the result in increasing order.
  1998. .VE
  1999. .TP
  2000. \fBopen \fIfileName\fR ?\fIaccess\fR?
  2001. .VS
  2002. Opens a file and returns an identifier
  2003. that may be used in future invocations
  2004. of commands like \fBread\fR, \fBputs\fR, and \fBclose\fR.
  2005. \fIFileName\fR gives the name of the file to open; if it starts with
  2006. a tilde then tilde substitution is performed as described for
  2007. \fBTcl_TildeSubst\fR.
  2008. If the first character of \fIfileName\fR is ``|'' then the
  2009. remaining characters of \fIfileName\fR are treated as a command
  2010. pipeline to invoke, in the same style as for \fBexec\fR.
  2011. In this case, the identifier returned by \fBopen\fR may be used
  2012. to write to the command's input pipe or read from its output pipe.
  2013. The \fIaccess\fR argument indicates the way in which the file
  2014. (or command pipeline) is to be accessed.
  2015. It may have any of the following values:
  2016. .RS
  2017. .TP
  2018. \fBr\fR
  2019. Open the file for reading only; the file must already exist.
  2020. .TP
  2021. \fBr+\fR
  2022. Open the file for both reading and writing; the file must
  2023. already exist.
  2024. .TP
  2025. \fBw\fR
  2026. Open the file for writing only.  Truncate it if it exists.  If it doesn't
  2027. exist, create a new file.
  2028. .TP
  2029. \fBw+\fR
  2030. Open the file for reading and writing.  Truncate it if it exists.
  2031. If it doesn't exist, create a new file.
  2032. .TP
  2033. \fBa\fR
  2034. Open the file for writing only.  The file must already exist, and the file
  2035. is positioned so that new data is appended to the file.
  2036. .TP
  2037. \fBa+\fR
  2038. Open the file for reading and writing.  If the file doesn't exist,
  2039. create a new empty file.
  2040. Set the initial access position  to the end of the file.
  2041. .PP
  2042. \fIAccess\fR defaults to \fBr\fR.
  2043. If a file is opened for both reading and writing, then \fBseek\fR
  2044. must be invoked between a read and a write, or vice versa (this
  2045. restriction does not apply to command pipelines opened with \fBopen\fR).
  2046. When \fIfileName\fR specifies a command pipeline and a write-only access
  2047. is used, then standard output from the pipeline is directed to the
  2048. current standard output unless overridden by the command.
  2049. When \fIfileName\fR specifies a command pipeline and a read-only access
  2050. is used, then standard input from the pipeline is taken from the
  2051. current standard input unless overridden by the command.
  2052. .RE
  2053. .VE
  2054. .TP
  2055. \fBproc \fIname args body\fR
  2056. The \fBproc\fR command creates a new Tcl command procedure,
  2057. \fIname\fR, replacing
  2058. any existing command there may have been by that name.  Whenever the
  2059. new command is invoked, the contents of \fIbody\fR will be executed
  2060. by the Tcl interpreter.  \fIArgs\fR specifies the formal arguments to the
  2061. procedure.  It consists of a list, possibly empty, each of whose
  2062. elements specifies
  2063. one argument.  Each argument specifier is also a list with either
  2064. one or two fields.  If there is only a single field in the specifier,
  2065. then it is the name of the argument; if there are two fields, then
  2066. the first is the argument name and the second is its default value.
  2067. braces and backslashes may be used in the usual way to specify
  2068. complex default values.
  2069. .IP
  2070. When \fIname\fR is invoked, a local variable
  2071. will be created for each of the formal arguments to the procedure; its
  2072. value will be the value of corresponding argument in the invoking command
  2073. or the argument's default value.
  2074. Arguments with default values need not be
  2075. specified in a procedure invocation.  However, there must be enough
  2076. actual arguments for all the
  2077. formal arguments that don't have defaults, and there must not be any extra
  2078. actual arguments.  There is one special case to permit procedures with
  2079. variable numbers of arguments.  If the last formal argument has the name
  2080. \fBargs\fR, then a call to the procedure may contain more actual arguments
  2081. than the procedure has formals.  In this case, all of the actual arguments
  2082. starting at the one that would be assigned to \fBargs\fR are combined into
  2083. a list (as if the \fBlist\fR command had been used); this combined value
  2084. is assigned to the local variable \fBargs\fR.
  2085. .IP
  2086. When \fIbody\fR is being executed, variable names normally refer to
  2087. local variables, which are created automatically when referenced and
  2088. deleted when the procedure returns.  One local variable is automatically
  2089. created for each of the procedure's arguments.
  2090. Global variables can only be accessed by invoking
  2091. the \fBglobal\fR command.
  2092. .IP
  2093. The \fBproc\fR command returns the null string.  When a procedure is
  2094. invoked, the procedure's return value is the value specified in a
  2095. \fBreturn\fR command.  If the procedure doesn't execute an explicit
  2096. \fBreturn\fR, then its return value is the value of the last command
  2097. executed in the procedure's body.
  2098. If an error occurs while executing the procedure
  2099. body, then the procedure-as-a-whole will return that same error.
  2100. .TP
  2101. \fBputs \fR?\fB\-nonewline\fR? ?\fIfileId\fR? \fIstring\fR
  2102. .VS
  2103. Writes the characters given by \fIstring\fR to the file given
  2104. by \fIfileId\fR.
  2105. \fIFileId\fR must have been the return
  2106. value from a previous call to \fBopen\fR, or it may be
  2107. \fBstdout\fR or \fBstderr\fR to refer to one of the standard I/O
  2108. channels; it must refer to a file that was opened for
  2109. writing.
  2110. If no \fIfileId\fR is specified then it defaults to \fBstdout\fR.
  2111. \fBPuts\fR normally outputs a newline character after \fIstring\fR,
  2112. .VS
  2113. but this feature may be suppressed by specifying the \fB\-nonewline\fR
  2114. switch.
  2115. .VE
  2116. Output to files is buffered internally by Tcl; the \fBflush\fR
  2117. command may be used to force buffered characters to be output.
  2118. .TP
  2119. \fBpwd\fR
  2120. .br
  2121. Returns the path name of the current working directory.
  2122. .TP
  2123. \fBread \fR?\fB\-nonewline\fR? \fIfileId\fR
  2124. .VS
  2125. .TP
  2126. \fBread \fIfileId numBytes\fR
  2127. In the first form, all of the remaining bytes are read from the file
  2128. given by \fIfileId\fR; they are returned as the result of the command.
  2129. If the \fB\-nonewline\fR switch is specified then the last
  2130. character of the file is discarded if it is a newline.
  2131. .VE
  2132. In the second form, the extra argument specifies how many bytes to read;
  2133. exactly this many bytes will be read and returned, unless there are fewer than
  2134. \fInumBytes\fR bytes left in the file; in this case, all the remaining
  2135. bytes are returned.
  2136. \fIFileId\fR must be \fBstdin\fR or the return
  2137. value from a previous call to \fBopen\fR; it must
  2138. refer to a file that was opened for reading.
  2139. .TP
  2140. \fBregexp \fR?\fB\-indices\fR? \fR?\fB\-nocase\fR? \fIexp string \fR?\fImatchVar\fR? ?\fIsubMatchVar subMatchVar ...\fR?
  2141. Determines whether the regular expression \fIexp\fR matches part or
  2142. all of \fIstring\fR and returns 1 if it does, 0 if it doesn't.
  2143. See REGULAR EXPRESSIONS above for complete information on the
  2144. syntax of \fIexp\fR and how it is matched against \fIstring\fR.
  2145. .RS
  2146. .LP
  2147. If the \fB\-nocase\fR switch is specified then upper-case
  2148. characters in \fIstring\fR
  2149. are treated as lower case during the matching process.
  2150. The \fB\-nocase\fR switch must be specified before \fIexp\fR and
  2151. may not be abbreviated.
  2152. .LP
  2153. If additional arguments are specified after \fIstring\fR then they
  2154. are treated as the names of variables to use to return
  2155. information about which part(s) of \fIstring\fR matched \fIexp\fR.
  2156. \fIMatchVar\fR will be set to the range of \fIstring\fR that
  2157. matched all of \fIexp\fR.  The first \fIsubMatchVar\fR will contain
  2158. the characters in \fIstring\fR that matched the leftmost parenthesized
  2159. subexpression within \fIexp\fR, the next \fIsubMatchVar\fR will
  2160. contain the characters that matched the next parenthesized
  2161. subexpression to the right in \fIexp\fR, and so on.
  2162. .LP
  2163. Normally, \fImatchVar\fR and the \fIsubMatchVar\fRs are set to hold
  2164. the matching characters from \fBstring\fR.
  2165. However, if the \fB\-indices\fR switch is specified then each variable
  2166. will contain a list of two decimal strings giving the indices
  2167. in \fIstring\fR of the first and last characters in the matching
  2168. range of characters.
  2169. The \fB\-indices\fR switch must be specified before the \fIexp\fR
  2170. argument and may not be abbreviated.
  2171. .LP
  2172. If there are more \fIsubMatchVar\fR's than parenthesized
  2173. subexpressions within \fIexp\fR, or if a particular subexpression
  2174. in \fIexp\fR doesn't match the string (e.g. because it was in a
  2175. portion of the expression that wasn't matched), then the corresponding
  2176. \fIsubMatchVar\fR will be set to ``\fB\-1 \-1\fR'' if \fB\-indices\fR
  2177. has been specified or to an empty string otherwise.
  2178. .RE
  2179. .TP
  2180. \fBregsub \fR?\fB\-all\fR? ?\fB\-nocase\fR? \fIexp string subSpec varName\fR
  2181. This command matches the regular expression \fIexp\fR against
  2182. \fIstring\fR using the rules described in REGULAR EXPRESSIONS
  2183. above.
  2184. If there is no match, then the command returns 0 and does nothing
  2185. else.
  2186. If there is a match, then the command returns 1 and also copies
  2187. \fIstring\fR to the variable whose name is given by \fIvarName\fR.
  2188. When copying \fIstring\fR, the portion of \fIstring\fR that
  2189. matched \fIexp\fR is replaced with \fIsubSpec\fR.
  2190. If \fIsubSpec\fR contains a ``&'' or ``\e0'', then it is replaced
  2191. in the substitution with the portion of \fIstring\fR that
  2192. matched \fIexp\fR.
  2193. If \fIsubSpec\fR contains a ``\e\fIn\fR'', where \fIn\fR is a digit
  2194. between 1 and 9, then it is replaced in the substitution with
  2195. the portion of \fIstring\fR that matched the \fIn\fR-th
  2196. parenthesized subexpression of \fIexp\fR.
  2197. Additional backslashes may be used in \fIsubSpec\fR to prevent special
  2198. interpretation of ``&'' or ``\e0'' or ``\e\fIn\fR'' or
  2199. backslash.
  2200. The use of backslashes in \fIsubSpec\fR tends to interact badly
  2201. with the Tcl parser's use of backslashes, so it's generally
  2202. safest to enclose \fIsubSpec\fR in braces if it includes
  2203. backslashes.
  2204. If the \fB\-all\fR argument is specified, then all ranges in
  2205. \fIstring\fR that match \fIexp\fR are found and substitution is
  2206. performed for each of these ranges;  otherwise only the first
  2207. matching range is found and substituted.
  2208. If \fB\-all\fR is specified, then ``&'' and ``\e\fIn\fR''
  2209. sequences are handled for each substitution using the information
  2210. from the corresponding match.
  2211. If the \fB\-nocase\fR argument is specified, then upper-case
  2212. characters in \fIstring\fR are converted to lower-case before
  2213. matching against \fIexp\fR;  however, substitutions specified
  2214. by \fIsubSpec\fR use the original unconverted form of \fIstring\fR.
  2215. The \fB\-all\fR and \fB\-nocase\fR arguments must be specified
  2216. exactly:  no abbreviations are permitted.
  2217. .VE
  2218. .TP
  2219. \fBrename \fIoldName newName\fR
  2220. Rename the command that used to be called \fIoldName\fR so that it
  2221. is now called \fInewName\fR.  If \fInewName\fR is an empty string
  2222. (e.g. {}) then \fIoldName\fR is deleted.  The \fBrename\fR command
  2223. returns an empty string as result.
  2224. .TP
  2225. \fBreturn \fR?\fIvalue\fR?
  2226. Return immediately from the current procedure
  2227. (or top-level command or \fBsource\fR command),
  2228. with \fIvalue\fR as the return value.  If \fIvalue\fR is not specified,
  2229. an empty string will be returned as result.
  2230. .TP
  2231. \fBscan \fIstring format varname1 \fR?\fIvarname2 ...\fR?
  2232. This command parses fields from an input string in the same fashion
  2233. as the C \fBsscanf\fR procedure.  \fIString\fR gives the input to
  2234. be parsed and \fIformat\fR indicates how to parse it, using \fB%\fR
  2235. fields as in \fBsscanf\fR.  All of the \fBsscanf\fR options are valid;
  2236. see the \fBsscanf\fR man page for details.  Each \fIvarname\fR gives
  2237. the name of a variable; when a field is scanned from \fIstring\fR,
  2238. the result is converted back into a string and assigned to the
  2239. corresponding \fIvarname\fR.  The only unusual conversion is for
  2240. \fB%c\fR.  For \fB%c\fR conversions a single character value is
  2241. converted to a decimal string, which is then assigned to the
  2242. corresponding \fIvarname\fR;
  2243. .VS
  2244. no field width may be specified for this conversion.
  2245. .TP
  2246. \fBseek \fIfileId offset \fR?\fIorigin\fR?
  2247. Change the current access position for \fIfileId\fR.
  2248. The \fIoffset\fR and \fIorigin\fR arguments specify the position at
  2249. which the next read or write will occur for \fIfileId\fR.
  2250. \fIOffset\fR must be a number (which may be negative) and \fIorigin\fR
  2251. must be one of the following:
  2252. .RS
  2253. .TP
  2254. \fBstart\fR
  2255. The new access position will be \fIoffset\fR bytes from the start
  2256. of the file.
  2257. .TP
  2258. \fBcurrent\fR
  2259. The new access position will be \fIoffset\fR bytes from the current
  2260. access position; a negative \fIoffset\fR moves the access position
  2261. backwards in the file.
  2262. .TP
  2263. \fBend\fR
  2264. The new access position will be \fIoffset\fR bytes from the end of
  2265. the file.  A negative \fIoffset\fR places the access position before
  2266. the end-of-file, and a positive \fIoffset\fR places the access position
  2267. after the end-of-file.
  2268. .LP
  2269. The \fIorigin\fR argument defaults to \fBstart\fR.
  2270. \fIFileId\fR must have been the return
  2271. value from a previous call to \fBopen\fR, or it may be \fBstdin\fR,
  2272. \fBstdout\fR, or \fBstderr\fR to refer to one of the standard I/O
  2273. channels.
  2274. This command returns an empty string.
  2275. .RE
  2276. .VE
  2277. .TP
  2278. \fBset \fIvarname \fR?\fIvalue\fR?
  2279. Returns the value of variable \fIvarname\fR.
  2280. If \fIvalue\fR is specified, then set
  2281. the value of \fIvarname\fR to \fIvalue\fR, creating a new variable
  2282. if one doesn't already exist, and return its value.
  2283. .VS
  2284. If \fIvarName\fR contains an open parenthesis and ends with a
  2285. close parenthesis, then it refers to an array element:  the characters
  2286. before the open parenthesis are the name of the array, and the characters
  2287. between the parentheses are the index within the array.
  2288. Otherwise \fIvarName\fR refers to a scalar variable.
  2289. .VE
  2290. If no procedure is active, then \fIvarname\fR refers to a global
  2291. variable.
  2292. If a procedure is active, then \fIvarname\fR refers to a parameter
  2293. or local variable of the procedure, unless the \fIglobal\fR command
  2294. has been invoked to declare \fIvarname\fR to be global.
  2295. .TP
  2296. \fBsource \fIfileName\fR
  2297. Read file \fIfileName\fR and pass the contents to the Tcl interpreter
  2298. as a sequence of commands to execute in the normal fashion.  The return
  2299. value of \fBsource\fR is the return value of the last command executed
  2300. from the file.  If an error occurs in executing the contents of the
  2301. file, then the \fBsource\fR command will return that error.
  2302. If a \fBreturn\fR command is invoked from within the file, the remainder of
  2303. the file will be skipped and the \fBsource\fR command will return
  2304. normally with the result from the \fBreturn\fR command.
  2305. If \fIfileName\fR starts with a tilde, then it is tilde-substituted
  2306. as described in the \fBTcl_TildeSubst\fR manual entry.
  2307. .TP
  2308. \fBsplit \fIstring \fR?\fIsplitChars\fR?
  2309. Returns a list created by splitting \fIstring\fR at each character
  2310. that is in the \fIsplitChars\fR argument.
  2311. Each element of the result list will consist of the
  2312. characters from \fIstring\fR between instances of the
  2313. characters in \fIsplitChars\fR.
  2314. Empty list elements will be generated if \fIstring\fR contains
  2315. adjacent characters in \fIsplitChars\fR, or if the first or last
  2316. character of \fIstring\fR is in \fIsplitChars\fR.
  2317. If \fIsplitChars\fR is an empty string then each character of
  2318. \fIstring\fR becomes a separate element of the result list.
  2319. \fISplitChars\fR defaults to the standard white-space characters.
  2320. For example,
  2321. .RS
  2322. .DS
  2323. \fBsplit "comp.unix.misc" .\fR
  2324. .DE
  2325. returns \fB"comp unix misc"\fR and
  2326. .DS
  2327. \fBsplit "Hello world" {}\fR
  2328. .DE
  2329. returns \fB"H e l l o { } w o r l d"\fR.
  2330. .VE
  2331. .RE
  2332. .TP
  2333. \fBstring \fIoption arg \fR?\fIarg ...?\fR
  2334. Perform one of several string operations, depending on \fIoption\fR.
  2335. The legal \fIoption\fRs (which may be abbreviated) are:
  2336. .RS
  2337. .TP
  2338. \fBstring compare \fIstring1 string2\fR
  2339. Perform a character-by-character comparison of strings \fIstring1\fR and
  2340. \fIstring2\fR in the same way as the C \fBstrcmp\fR procedure.  Return
  2341. -1, 0, or 1, depending on whether \fIstring1\fR is lexicographically
  2342. less than, equal to, or greater than \fIstring2\fR.
  2343. .TP
  2344. \fBstring first \fIstring1 string2\fR
  2345. Search \fIstring2\fR for a sequence of characters that exactly match
  2346. the characters in \fIstring1\fR.  If found, return the index of the
  2347. first character in the first such match within \fIstring2\fR.  If not
  2348. found, return -1.
  2349. .br
  2350. .VS
  2351. .TP
  2352. \fBstring index \fIstring charIndex\fR
  2353. Returns the \fIcharIndex\fR'th character of the \fIstring\fR
  2354. argument.  A \fIcharIndex\fR of 0 corresponds to the first
  2355. character of the string.
  2356. If \fIcharIndex\fR is less than 0 or greater than
  2357. or equal to the length of the string then an empty string is
  2358. returned.
  2359. .VE
  2360. .TP
  2361. \fBstring last \fIstring1 string2\fR
  2362. Search \fIstring2\fR for a sequence of characters that exactly match
  2363. the characters in \fIstring1\fR.  If found, return the index of the
  2364. first character in the last such match within \fIstring2\fR.  If there
  2365. is no match, then return \-1.
  2366. .br
  2367. .VS
  2368. .TP
  2369. \fBstring length \fIstring\fR
  2370. Returns a decimal string giving the number of characters in \fIstring\fR.
  2371. .VE
  2372. .TP
  2373. \fBstring match \fIpattern\fR \fIstring\fR
  2374. See if \fIpattern\fR matches \fIstring\fR; return 1 if it does, 0
  2375. if it doesn't.  Matching is done in a fashion similar to that
  2376. used by the C-shell.  For the two strings to match, their contents
  2377. must be identical except that the following special sequences
  2378. may appear in \fIpattern\fR:
  2379. .RS
  2380. .IP \fB*\fR 10
  2381. Matches any sequence of characters in \fIstring\fR,
  2382. including a null string.
  2383. .IP \fB?\fR 10
  2384. Matches any single character in \fIstring\fR.
  2385. .IP \fB[\fIchars\fB]\fR 10
  2386. Matches any character in the set given by \fIchars\fR.  If a sequence
  2387. of the form
  2388. \fIx\fB\-\fIy\fR appears in \fIchars\fR, then any character
  2389. between \fIx\fR and \fIy\fR, inclusive, will match.
  2390. .IP \fB\e\fIx\fR 10
  2391. Matches the single character \fIx\fR.  This provides a way of
  2392. avoiding the special interpretation of the characters
  2393. \fB*?[]\e\fR in \fIpattern\fR.
  2394. .RE
  2395. .br
  2396. .VS
  2397. .TP
  2398. \fBstring range \fIstring first last\fR
  2399. Returns a range of consecutive characters from \fIstring\fR, starting
  2400. with the character whose index is \fIfirst\fR and ending with the
  2401. character whose index is \fIlast\fR.  An index of 0 refers to the
  2402. first character of the string.  \fILast\fR may be \fBend\fR (or any
  2403. abbreviation of it) to refer to the last character of the string.
  2404. If \fIfirst\fR is less than zero then it is treated as if it were zero, and
  2405. if \fIlast\fR is greater than or equal to the length of the string then
  2406. it is treated as if it were \fBend\fR.  If \fIfirst\fR is greater than
  2407. \fIlast\fR then an empty string is returned.
  2408. .TP
  2409. \fBstring tolower \fIstring\fR
  2410. Returns a value equal to \fIstring\fR except that all upper case
  2411. letters have been converted to lower case.
  2412. .TP
  2413. \fBstring toupper \fIstring\fR
  2414. Returns a value equal to \fIstring\fR except that all lower case
  2415. letters have been converted to upper case.
  2416. .TP
  2417. \fBstring trim \fIstring\fR ?\fIchars\fR?
  2418. Returns a value equal to \fIstring\fR except that any leading
  2419. or trailing characters from the set given by \fIchars\fR are
  2420. removed.
  2421. If \fIchars\fR is not specified then white space is removed
  2422. (spaces, tabs, newlines, and carriage returns).
  2423. .TP
  2424. \fBstring trimleft \fIstring\fR ?\fIchars\fR?
  2425. Returns a value equal to \fIstring\fR except that any
  2426. leading characters from the set given by \fIchars\fR are
  2427. removed.
  2428. If \fIchars\fR is not specified then white space is removed
  2429. (spaces, tabs, newlines, and carriage returns).
  2430. .TP
  2431. \fBstring trimright \fIstring\fR ?\fIchars\fR?
  2432. Returns a value equal to \fIstring\fR except that any
  2433. trailing characters from the set given by \fIchars\fR are
  2434. removed.
  2435. If \fIchars\fR is not specified then white space is removed
  2436. (spaces, tabs, newlines, and carriage returns).
  2437. .RE
  2438. .TP
  2439. \fBtell \fIfileId\fR
  2440. Returns a decimal string giving the current access position in
  2441. \fIfileId\fR.
  2442. \fIFileId\fR must have been the return
  2443. value from a previous call to \fBopen\fR, or it may be \fBstdin\fR,
  2444. \fBstdout\fR, or \fBstderr\fR to refer to one of the standard I/O
  2445. channels.
  2446. .VE
  2447. .TP
  2448. \fBtime \fIcommand\fR ?\fIcount\fR?
  2449. This command will call the Tcl interpreter \fIcount\fR
  2450. times to execute \fIcommand\fR (or once if \fIcount\fR isn't
  2451. specified).  It will then return a string of the form
  2452. .RS
  2453. .DS
  2454. \fB503 microseconds per iteration\fR
  2455. .DE
  2456. which indicates the average amount of time required per iteration,
  2457. in microseconds.
  2458. Time is measured in elapsed time, not CPU time.
  2459. .RE
  2460. .TP
  2461. \fBtrace \fIoption\fR ?\fIarg arg ...\fR?
  2462. .VS
  2463. Cause Tcl commands to be executed whenever certain operations are
  2464. invoked.  At present, only variable tracing is implemented. The
  2465. legal \fIoption\fR's (which may be abbreviated) are:
  2466. .RS
  2467. .TP
  2468. \fBtrace variable \fIname ops command\fR
  2469. Arrange for \fIcommand\fR to be executed whenever variable \fIname\fR
  2470. is accessed in one of the ways given by \fIops\fR.  \fIName\fR may
  2471. refer to a normal variable, an element of an array, or to an array
  2472. as a whole (i.e. \fIname\fR may be just the name of an array, with no
  2473. parenthesized index).  If \fIname\fR refers to a whole array, then
  2474. \fIcommand\fR is invoked whenever any element of the array is
  2475. manipulated.
  2476. .RS
  2477. .LP
  2478. \fIOps\fR indicates which operations are of interest, and consists of
  2479. one or more of the following letters:
  2480. .RS
  2481. .TP
  2482. \fBr\fR
  2483. Invoke \fIcommand\fR whenever the variable is read.
  2484. .TP
  2485. \fBw\fR
  2486. Invoke \fIcommand\fR whenever the variable is written.
  2487. .TP
  2488. \fBu\fR
  2489. Invoke \fIcommand\fR whenever the variable is unset.  Variables
  2490. can be unset explicitly with the \fBunset\fR command, or
  2491. implicitly when procedures return (all of their local variables
  2492. are unset).  Variables are also unset when interpreters are
  2493. deleted, but traces will not be invoked because there is no
  2494. interpreter in which to execute them.
  2495. .RE
  2496. .LP
  2497. When the trace triggers, three arguments are appended to
  2498. \fIcommand\fR so that the actual command is as follows:
  2499. .DS C
  2500. \fIcommand name1 name2 op\fR
  2501. .DE
  2502. \fIName1\fR and \fIname2\fR give the name(s) for the variable
  2503. being accessed:  if the variable is a scalar then \fIname1\fR
  2504. gives the variable's name and \fIname2\fR is an empty string;
  2505. if the variable is an array element then \fIname1\fR gives the
  2506. name of the array and name2 gives the index into the array;
  2507. if an entire array is being deleted and the trace was registered
  2508. on the overall array, rather than a single element, then \fIname1\fR
  2509. gives the array name and \fIname2\fR is an empty string.
  2510. \fIOp\fR indicates what operation is being performed on the
  2511. variable, and is one of \fBr\fR, \fBw\fR, or \fBu\fR as
  2512. defined above.
  2513. .LP
  2514. \fICommand\fR executes in the same context as the code that invoked
  2515. the traced operation:  if the variable was accessed as part of a
  2516. Tcl procedure, then \fIcommand\fR will have access to the same
  2517. local variables as code in the procedure.  This context may be
  2518. different than the context in which the trace was created.
  2519. If \fIcommand\fR invokes a procedure (which it normally does) then
  2520. the procedure will have to use \fBupvar\fR or \fBuplevel\fR if it
  2521. wishes to access the traced variable.
  2522. Note also that \fIname1\fR may not necessarily be the same as the name
  2523. used to set the trace on the variable;  differences can occur if
  2524. the access is made through a variable defined with the \fBupvar\fR
  2525. command.
  2526. .LP
  2527. For read and write traces, \fIcommand\fR can modify
  2528. the variable to affect the result of the traced operation.
  2529. If \fIcommand\fR modifies the value of a variable during a
  2530. read or write trace, then the new value will be returned as the
  2531. result of the traced operation.
  2532. The return value from  \fIcommand\fR is ignored except that
  2533. if it returns an error of any sort then the traced operation
  2534. is aborted with an error message saying that the access was denied
  2535. (this mechanism can be used to implement read-only variables, for
  2536. example).
  2537. For write traces, \fIcommand\fR is invoked after the variable's
  2538. value has been changed; it can write a new value into the variable
  2539. to override the original value specified in the write operation.
  2540. To implement read-only variables, \fIcommand\fR will have to restore
  2541. the old value of the variable.
  2542. .LP
  2543. While \fIcommand\fR is executing during a read or write trace, traces
  2544. on the variable are temporarily disabled.
  2545. This means that reads and writes invoked by
  2546. \fIcommand\fR will occur directly, without invoking \fIcommand\fR
  2547. (or any other traces) again.
  2548. .LP
  2549. When an unset trace is invoked, the variable has already been
  2550. deleted:  it will appear to be undefined with no traces.
  2551. If an unset occurs because of a procedure return, then the
  2552. trace will be invoked in the variable context of the procedure
  2553. being returned to:  the stack frame of the returning procedure
  2554. will no longer exist.
  2555. Traces are not disabled during unset traces, so if an unset trace
  2556. command creates a new trace and accesses the variable, the
  2557. trace will be invoked.
  2558. .LP
  2559. If there are multiple traces on a variable they are invoked
  2560. in order of creation, most-recent first.
  2561. If one trace returns an error, then no further traces are
  2562. invoked for the variable.
  2563. If an array element has a trace set, and there is also a trace
  2564. set on the array as a whole, the trace on the overall array
  2565. is invoked before the one on the element.
  2566. .LP
  2567. Once created, the trace remains in effect either until the
  2568. trace is removed with the \fBtrace vdelete\fR command described
  2569. below, until the variable is unset, or until the interpreter
  2570. is deleted.
  2571. Unsetting an element of array will remove any traces on that
  2572. element, but will not remove traces on the overall array.
  2573. .LP
  2574. This command returns an empty string.
  2575. .RE
  2576. .TP
  2577. \fBtrace vdelete \fIname ops command\fR
  2578. If there is a trace set on variable \fIname\fR with the
  2579. operations and command given by \fIops\fR and \fIcommand\fR,
  2580. then the trace is removed, so that \fIcommand\fR will never
  2581. again be invoked.
  2582. Returns an empty string.
  2583. .TP
  2584. \fBtrace vinfo \fIname\fR
  2585. Returns a list containing one element for each trace
  2586. currently set on variable \fIname\fR.
  2587. Each element of the list is itself a list containing two
  2588. elements, which are the \fIops\fR and \fIcommand\fR associated
  2589. with the trace.
  2590. If \fIname\fR doesn't exist or doesn't have any traces set, then
  2591. the result of the command will be an empty string.
  2592. .RE
  2593. .TP
  2594. \fBunknown \fIcmdName \fR?\fIarg arg ...\fR?
  2595. This command doesn't actually exist as part of Tcl, but Tcl will
  2596. invoke it if it does exist.
  2597. If the Tcl interpreter encounters a command name for which there
  2598. is not a defined command, then Tcl checks for the existence of
  2599. a command named \fBunknown\fR.
  2600. If there is no such command, then the interpeter returns an
  2601. error.
  2602. If the \fBunknown\fR command exists, then it is invoked with
  2603. arguments consisting of the fully-substituted name and arguments
  2604. for the original non-existent command.
  2605. The \fBunknown\fR command typically does things like searching
  2606. through library directories for a command procedure with the name
  2607. \fIcmdName\fR, or expanding abbreviated command names to full-length,
  2608. or automatically executing unknown commands as UNIX sub-processes.
  2609. In some cases (such as expanding abbreviations) \fBunknown\fR will
  2610. change the original command slightly and then (re-)execute it.
  2611. The result of the \fBunknown\fR command is used as the result for
  2612. the original non-existent command.
  2613. .TP
  2614. \fBunset \fIname \fR?\fIname name ...\fR?
  2615. Remove one or more variables.
  2616. Each \fIname\fR is a variable name, specified in any of the
  2617. ways acceptable to the \fBset\fR command.
  2618. If a \fIname\fR refers to an element of an array, then that
  2619. element is removed without affecting the rest of the array.
  2620. If a \fIname\fR consists of an array name with no parenthesized
  2621. index, then the entire array is deleted.
  2622. The \fBunset\fR command returns an empty string as result.
  2623. An error occurs if any of the variables doesn't exist.
  2624. .VE
  2625. .TP
  2626. \fBuplevel \fR?\fIlevel\fR?\fI command \fR?\fIcommand ...\fR?
  2627. All of the \fIcommand\fR arguments are concatenated as if they had
  2628. been passed to \fBconcat\fR; the result is then evaluated in the
  2629. variable context indicated by \fIlevel\fR.  \fBUplevel\fR returns
  2630. the result of that evaluation.  If \fIlevel\fR is an integer, then
  2631. it gives a distance (up the procedure calling stack) to move before
  2632. executing the command.  If \fIlevel\fR consists of \fB#\fR followed by
  2633. a number then the number gives an absolute level number.  If \fIlevel\fR
  2634. is omitted then it defaults to \fB1\fR.  \fILevel\fR cannot be
  2635. defaulted if the first \fIcommand\fR argument starts with a digit or \fB#\fR.
  2636. For example, suppose that procedure \fBa\fR was invoked
  2637. from top-level, and that it called \fBb\fR, and that \fBb\fR called \fBc\fR.
  2638. Suppose that \fBc\fR invokes the \fBuplevel\fR command.  If \fIlevel\fR
  2639. is \fB1\fR or \fB#2\fR  or omitted, then the command will be executed
  2640. in the variable context of \fBb\fR.  If \fIlevel\fR is \fB2\fR or \fB#1\fR
  2641. then the command will be executed in the variable context of \fBa\fR.
  2642. If \fIlevel\fR is \fB3\fR or \fB#0\fR then the command will be executed
  2643. at top-level (only global variables will be visible).
  2644. The \fBuplevel\fR command causes the invoking procedure to disappear
  2645. from the procedure calling stack while the command is being executed.
  2646. In the above example, suppose \fBc\fR invokes the command
  2647. .RS
  2648. .DS
  2649. \fBuplevel 1 {set x 43; d}
  2650. .DE
  2651. where \fBd\fR is another Tcl procedure.  The \fBset\fR command will
  2652. modify the variable \fBx\fR in \fBb\fR's context, and \fBd\fR will execute
  2653. at level 3, as if called from \fBb\fR.  If it in turn executes
  2654. the command
  2655. .DS
  2656. \fBuplevel {set x 42}
  2657. .DE
  2658. then the \fBset\fR command will modify the same variable \fBx\fR in \fBb\fR's
  2659. context:  the procedure \fBc\fR does not appear to be on the call stack
  2660. when \fBd\fR is executing.  The command ``\fBinfo level\fR'' may
  2661. be used to obtain the level of the current procedure.
  2662. \fBUplevel\fR makes it possible to implement new control
  2663. constructs as Tcl procedures (for example, \fBuplevel\fR could
  2664. be used to implement the \fBwhile\fR construct as a Tcl procedure).
  2665. .RE
  2666. .TP
  2667. \fBupvar \fR?\fIlevel\fR? \fIotherVar myVar \fR?\fIotherVar myVar \fR...?
  2668. .VS
  2669. This command arranges for one or more local variables in the current
  2670. procedure to refer to variables in an enclosing procedure call or
  2671. to global variables.
  2672. \fILevel\fR may have any of the forms permitted for the \fBuplevel\fR
  2673. command, and may be omitted if the first letter of the first \fIotherVar\fR
  2674. isn't \fB#\fR or a digit (it defaults to \fB1\fR).
  2675. For each \fIotherVar\fR argument, \fBupvar\fR makes the variable
  2676. by that name in the procedure frame given by \fIlevel\fR (or at
  2677. global level, if \fIlevel\fR is \fB#0\fR) accessible
  2678. in the current procedure by the name given in the corresponding
  2679. \fImyVar\fR argument.
  2680. The variable named by \fIotherVar\fR need not exist at the time of the
  2681. call;  it will be created the first time \fImyVar\fR is referenced, just like
  2682. an ordinary variable.
  2683. \fBUpvar\fR may only be invoked from within procedures.
  2684. Neither \fIotherVar\fR or \fImyVar\fR may refer to an element of an
  2685. array.
  2686. \fBUpvar\fR returns an empty string.
  2687. .RS
  2688. .LP
  2689. The \fBupvar\fR command simplifies the implementation of call-by-name
  2690. procedure calling and also makes it easier to build new control constructs
  2691. as Tcl procedures.
  2692. For example, consider the following procedure:
  2693. .DS
  2694. .ta 1c 2c 3c
  2695. \fBproc add2 name {
  2696.     upvar $name x
  2697.     set x [expr $x+2]
  2698. }
  2699. .DE
  2700. \fBAdd2\fR is invoked with an argument giving the name of a variable,
  2701. and it adds two to the value of that variable.
  2702. Although \fBadd2\fR could have been implemented using \fBuplevel\fR
  2703. instead of \fBupvar\fR, \fBupvar\fR makes it simpler for \fBadd2\fR
  2704. to access the variable in the caller's procedure frame.
  2705. .VE
  2706. .RE
  2707. .TP
  2708. \fBwhile \fItest body
  2709. .VS
  2710. The \fIwhile\fR command evaluates \fItest\fR as an expression
  2711. (in the same way that \fBexpr\fR evaluates its argument).
  2712. The value of the expression must be numeric; if it is non-zero
  2713. then \fIbody\fR is executed by passing it to the Tcl interpreter.
  2714. Once \fIbody\fR has been executed then \fItest\fR is evaluated
  2715. again, and the process repeats until eventually \fItest\fR
  2716. evaluates to a zero numeric value.  \fBContinue\fR
  2717. commands may be executed inside \fIbody\fR to terminate the current
  2718. iteration of the loop, and \fBbreak\fR
  2719. commands may be executed inside \fIbody\fR to cause immediate
  2720. termination of the \fBwhile\fR command.  The \fBwhile\fR command
  2721. always returns an empty string.
  2722. .VE
  2723.  
  2724. .SH "BUILT-IN VARIABLES"
  2725. .PP
  2726. The following global variables are created and managed automatically
  2727. by the Tcl library.  Except where noted below, these variables should
  2728. normally be treated as read-only by application-specific code and by users.
  2729. .TP
  2730. \fBenv\fR
  2731. .br
  2732. .VS
  2733. This variable is maintained by Tcl as an array
  2734. whose elements are the environment variables for the process.
  2735. Reading an element will return the value of the corresponding
  2736. environment variable.
  2737. Setting an element of the array will modify the corresponding
  2738. environment variable or create a new one if it doesn't already
  2739. exist.
  2740. Unsetting an element of \fBenv\fR will remove the corresponding
  2741. environment variable.
  2742. Changes to the \fBenv\fR array will affect the environment
  2743. passed to children by commands like \fBexec\fR.
  2744. If the entire \fBenv\fR array is unset then Tcl will stop
  2745. monitoring \fBenv\fR accesses and will not update environment
  2746. variables.
  2747. .TP
  2748. \fBerrorCode\fR
  2749. After an error has occurred, this variable will be set to hold
  2750. additional information about the error in a form that is easy
  2751. to process with programs.
  2752. \fBerrorCode\fR consists of a Tcl list with one or more elements.
  2753. The first element of the list identifies a general class of
  2754. errors, and determines the format of the rest of the list.
  2755. The following formats for \fBerrorCode\fR are used by the
  2756. Tcl core; individual applications may define additional formats.
  2757. .RS
  2758. .TP
  2759. \fBCHILDKILLED\fI pid sigName msg\fR
  2760. This format is used when a child process has been killed because of
  2761. a signal.  The second element of \fBerrorCode\fR will be the
  2762. process's identifier (in decimal).
  2763. The third element will be the symbolic name of the signal that caused
  2764. the process to terminate; it will be one of the names from the
  2765. include file signal.h, such as \fBSIGPIPE\fR.
  2766. The fourth element will be a short human-readable message
  2767. describing the signal, such as ``write on pipe with no readers''
  2768. for \fBSIGPIPE\fR.
  2769. .TP
  2770. \fBCHILDSTATUS\fI pid code\fR
  2771. This format is used when a child process has exited with a non-zero
  2772. exit status.  The second element of \fBerrorCode\fR will be the
  2773. process's identifier (in decimal) and the third element will be the exit
  2774. code returned by the process (also in decimal).
  2775. .TP
  2776. \fBCHILDSUSP\fI pid sigName msg\fR
  2777. This format is used when a child process has been suspended because
  2778. of a signal.
  2779. The second element of \fBerrorCode\fR will be the process's identifier,
  2780. in decimal.
  2781. The third element will be the symbolic name of the signal that caused
  2782. the process to suspend; this will be one of the names from the
  2783. include file signal.h, such as \fBSIGTTIN\fR.
  2784. The fourth element will be a short human-readable message
  2785. describing the signal, such as ``background tty read''
  2786. for \fBSIGTTIN\fR.
  2787. .TP
  2788. \fBNONE\fR
  2789. .br
  2790. This format is used for errors where no additional information is
  2791. available for an error besides the message returned with the
  2792. error.  In these cases \fBerrorCode\fR will consist of a list
  2793. containing a single element whose contents are \fBNONE\fR.
  2794. .TP
  2795. \fBUNIX \fIerrName msg\fR
  2796. If the first element of \fBerrorCode\fR is \fBUNIX\fR, then
  2797. the error occurred during a UNIX kernel call.
  2798. The second element of the list will contain the symbolic name
  2799. of the error that occurred, such as \fBENOENT\fR; this will
  2800. be one of the values defined in the include file errno.h.
  2801. The third element of the list will be a human-readable
  2802. message corresponding to \fIerrName\fR, such as
  2803. ``no such file or directory'' for the \fBENOENT\fR case.
  2804. .PP
  2805. To set \fBerrorCode\fR, applications should use library
  2806. procedures such as \fBTcl_SetErrorCode\fR and
  2807. \fBTcl_UnixError\fR, or they may invoke the \fBerror\fR command.
  2808. If one of these methods hasn't been used, then the Tcl
  2809. interpreter will reset the variable to \fBNONE\fR after
  2810. the next error.
  2811. .RE
  2812. .VE
  2813. .TP
  2814. \fBerrorInfo\fR
  2815. After an error has occurred, this string will contain one or more lines
  2816. identifying the Tcl commands and procedures that were being executed
  2817. when the most recent error occurred.
  2818. Its contents take the form of a stack trace showing the various
  2819. nested Tcl commands that had been invoked at the time of the error.
  2820.  
  2821. .SH AUTHOR
  2822. John Ousterhout, University of California at Berkeley (ouster@sprite.berkeley.edu)
  2823. .sp
  2824. Many people have contributed to Tcl in various ways, but the following
  2825. people have made unusually large contributions:
  2826. .sp
  2827. .nf
  2828. Bill Carpenter
  2829. Peter Da Silva
  2830. Mark Diekhans
  2831. Karl Lehenbauer
  2832. Mary Ann May-Pumphrey
  2833.